You are on page 1of 9

Linux Terminal Commands

1.1 pwd
pwd (print working directory) displays the current working directory of the
user. When we first log into our system, our current directory is set to our
home directory.
$ pwd
/ home / user

1.2 ls
ls is used to list the files and directories in current working directory.
$ ls
Desktop Documents Music Pictures Public Templates Videos

Besides current working directory we can also specify a directory to list,


$ ls / usr
bin games include lib local sbin share src

ls also has some options we can use,

• -l: to list the output in long format

• -a: to list all files, even the hidden ones

• -h: display file sizes in human redable form

• -S: sort the files by their size

• -t: sort the files by modification time

1
$ ls -l
drwxrwxr - x 2 user user 4096 2023 -9 -26 17:23 Desktop
drwxrwxr - x 2 user user 93920 2023 -9 -26 17:21 Documents
drwxrwxr - x 2 user user 2041 2023 -9 -26 17:22 Music
$ ls - al
drwxrwxr - x 2 user user 4096 2023 -9 -26 17:19 .
drwxrwxr - x 2 user user 4096 2023 -9 -26 17:20 ..
drwxrwxr - x 2 user user 4096 2023 -9 -26 17:23 Desktop
drwxrwxr - x 2 user user 93920 2023 -9 -26 17:21 Documents
drwxrwxr - x 2 user user 2041 2023 -9 -26 17:22 Music
$ ls - lt
drwxrwxr - x 2 user user 4096 2023 -9 -26 17:23 Desktop
drwxrwxr - x 2 user user 2041 2023 -9 -26 17:22 Music
drwxrwxr - x 2 user user 93920 2023 -9 -26 17:21 Documents
$ ls - lS
drwxrwxr - x 2 user user 93920 2023 -9 -26 17:21 Documents
drwxrwxr - x 2 user user 4096 2023 -9 -26 17:23 Desktop
drwxrwxr - x 2 user user 2041 2023 -9 -26 17:22 Music
$ ls - lh
drwxrwxr - x 2 user user 4.0 K 2023 -9 -26 17:23 Desktop
drwxrwxr - x 2 user user 93 K 2023 -9 -26 17:21 Documents
drwxrwxr - x 2 user user 2.0 K 2023 -9 -26 17:22 Music

1.3 cd
cd changes our current working directory to the specified directory or path.
$ cd / usr / bin
$ pwd
/ usr / bin

1.4 cat
cat command reads one or more files and copies the content to the standard
output.
$ cat output . txt
The quick brown fox jumped over the lazy dog .

cat also has some options we can use,


• -n: shows line numbers

2
• -s: compresses consecutive blank lines and only shows a single one

• -b: number nonempty lines only

$ cat output . txt


The quick brown fox

jumped over the lazy dog .


$ cat -n output . txt
1 The quick brown fox
2
3
4 jumped over the lazy dog .
$ cat - ns output . txt
1 The quick brown fox
2
3 jumped over the lazy dog .
$ cat - nb output . txt
1 The quick brown fox

2 jumped over the lazy dog .

1.5 I/O redirection


We can use > character to redirect the output of a command to a file.
$ cat - nsb output . txt > output2 . txt
$ cat output2 . txt
1 The quick brown fox

2 jumped over the lazy dog .

1.6 mkdir
mkdir command created new directory with specified file name or pathname.
$ mkdir test
$ ls
Desktop Documents Music Pictures Public Templates Videos test

3
If we specify -p option to the mkdir command, it will also create the
non-existing parent directories.
$ ls
Desktop Documents Music Pictures Public Templates Videos
$ mkdir -p dir1 / dir2 / dir3
$ ls
$ ls
Desktop Documents Music Pictures Public Templates Videos dir1
$ cd dir1
$ ls
dir2
$ cd dir2
$ ls
dir3

1.7 rm and rmdir


rm command deletes a file from the specified pathname and rmdir command
deletes a directory from the specified pathname.
$ ls
Desktop Documents Music Pictures Public Templates Videos a . txt
$ rm a . txt
$ ls
Desktop Documents Music Pictures Public Templates Videos

$ ls
Desktop Documents Music Pictures Public Templates Videos test
$ rmdir test
$ ls
Desktop Documents Music Pictures Public Templates Videos

1.8 cp
cp command copies specified file(s) contents to a specified file or pathname.

4
$ cat a . txt
The quick brown fox
$ cat b . txt
jumped over the lazy dog .
$ cp a . txt out . txt
$ cat out . txt
The quick brown fox
$ cat a . txt b . txt Documents
$ cd Documents
$ ls Documents
a . txt b . txt

cp command also has some useful options,

• -i: before overwriting an existing file, prompts the user for confirma-
tion

• -r: recursively directories and their contents

1.9 mv
mv command performs both moving and renaming operation.
$ ls
Desktop Documents Music Pictures Public Templates Videos test
$ mv test Documents
$ ls Documents
test
$ mv Templates Temps
$ ls
Desktop Documents Music Pictures Public Temps Videos

Similar to cp command, mv command also has an option -i. Which


prompts user before overwriting a directory.

1.9 less
less command shows the contents of specified file page by page. And also
provide with powerful navigation and searching capabilities.
$ less a . cpp

5
1.11 touch
touch command is usually used to change access, change and modify time
of a file. If the specified file does not exist, then touch command creates a
blank file with the specified name.
$ ls
Desktop Documents Music Pictures Public Templates Videos
$ touch newfile
$ ls - lh newfile
drwxrwxr - x 2 user user 0 B 2023 -9 -26 17:30 newfile
$ touch newfile
drwxrwxr - x 2 user user 2041 2023 -9 -26 17:32 newfile

1.12 nano
nano command starts up a basic editor. Where specifying a file, nano opens
up the file for editing with minimal features.

$ nano a . cpp

Figure 1: output of ’nano a.cpp’ command

6
1.13 sudo
sudo command lets regular user become super user to do administrative
operations or change system settings, install system wide softwares etc.
$ sudo apt install gcc

Above command installs gcc in the whole system.

1.14 top
top command displays a continuously updating (by default, every three sec-
onds) display of the system processes listed in order of process activity.

$ top

Figure 2: Output of ’top’ command

1.15 kill
kill command is used to send signals to programs. Such as, terminate,
stop, continue etc.
$ kill 2746

7
1.16 echo
echo command writes all the given strings to the standard output with spaces
in between and a newline after the last given string.
$ echo The quick brown fox jumped over the lazy dog .
The quick brown fox jumped over the lazy dog .

1.17 file
file command shows information about the file type of the specified file.
$ file a . cpp
a . cpp : C ++ source , ASCII text
$ file test . png
test . png : PNG image data , 563 x 137 , 8 - bit / color RGBA ,
non - interlaced

1.18 chmod
chmod command is used to change a file or directory read-write permission
for any user. Only the file’s owner or the superuser can change the mode
of a file or directory. chmod supports two distinct ways of specifying mode
changes: octal number representation, or symbolic representation
$ touch foo . txt
$ ls -l foo . txt
-rw - rw -r - - 1 user user 0 2023 -09 -26 17:52 foo . txt
$ chmod 600 foo . txt
$ ls -l foo . txt
-rw - - - - - - - 1 user user 0 2023 -09 -26 17:53 foo . txt
$ chmod 375 foo . txt
$ ls -l foo . txt
-- wxrwxr - x 1 user user 0 2023 -09 -26 17:53 foo . txt

1.19 ps
ps command shows the processes associated with the current terminal ses-
sion.

8
$ ps
PID TTY TIME CMD
4569 pts /1 00:00:04 zsh
16447 pts /1 00:00:00 ps

You might also like