Top 20 Must Know Basic Commands in Linux with Examples Part 1

Here’s the list of basic commands a Linux user  needs to know and familiarize  for them to feel comfortable in working with command line interface/shell/terminal. We all know that mostly all Linux distribution now comes with their own GUIs or Graphical User Interfaces but not can be managememanagement of tasks but not are good for all tasks especially when you need to do some 
I’ll break down into two parts for nicer readability and to emphasize with the examples for better understanding.

  1. ls
    – command to list directory contents
    Useful options with “ls”
    ls -l –> list files,directory,size,date/time,owner,permission

tux@freelinux:/usr/local$ ls -l
total 32
drwxr-xr-x 2 root root 4096 Jan 8 2018 bin
drwxr-xr-x 2 root root 4096 Jan 8 2018 etc
drwxr-xr-x 2 root root 4096 Jan 8 2018 games
drwxr-xr-x 2 root root 4096 Jan 8 2018 include
drwxr-xr-x 3 root root 4096 Feb 16 2018 lib
lrwxrwxrwx 1 root root 9 Feb 16 2018 man -> share/man
drwxr-xr-x 2 root root 4096 Jan 8 2018 sbin
drwxr-xr-x 4 root root 4096 Feb 16 2018 share
drwxr-xr-x 2 root root 4096 Jan 8 2018 src

ls -a –> list all files (including starting with .)

tux@freelinux:/usr/local$ ls -a

. .. bin etc games include lib man sbin share src

ls -s –> list in sorted order by file size

tux@freelinux:/usr/local$ ls -s
total 32
4 bin 4 etc 4 games 4 include 4 lib 0 man 4 sbin 4 share 4 src

ls -t –> list and sort by modification time, newest first

tux@freelinux:/usr/local$ ls -t
lib man share bin etc games include sbin src

ls -r –> list in reverse order

tux@freelinux:/usr/local$ ls -r
src share sbin man lib include games etc bin

ls -R –> list sub-directories recursively

tux@freelinux:/usr/local$ ls -R
.:
bin etc games include lib man sbin share src
./bin:
./etc:
./games:
./include:
./lib:
python3.6
./lib/python3.6:
dist-packages
./lib/python3.6/dist-packages:
./sbin:
./share:
ca-certificates man
./share/ca-certificates:
./share/man:
./src:


Sample combine options.
ls -ltr –> list files and show oldest first

tux@freelinux:/usr/local$ ls -ltr
total 32
drwxr-xr-x 2 root root 4096 Jan 8 2018 src
drwxr-xr-x 2 root root 4096 Jan 8 2018 sbin
drwxr-xr-x 2 root root 4096 Jan 8 2018 include
drwxr-xr-x 2 root root 4096 Jan 8 2018 games
drwxr-xr-x 2 root root 4096 Jan 8 2018 etc
drwxr-xr-x 2 root root 4096 Jan 8 2018 bin
drwxr-xr-x 4 root root 4096 Feb 16 2018 share
lrwxrwxrwx 1 root root 9 Feb 16 2018 man -> share/man
drwxr-xr-x 3 root root 4096 Feb 16 2018 lib

 

2. pwd
– command to print name of current/working directory

tux@freelinux:~$ pwd
/home/tux

 

3. cd
– command to allow user to change directories.
Useful options with ‘cd’ (I’ll be adding the pwd command to understand the current directory)
cd [full path]

tux@freelinux:~$ pwd
/home/tux
tux@freelinux:~$ cd /usr/local
tux@freelinux:/usr/local$ pwd
/usr/local


cd ~ –> change to home directory ( or just type ‘cd’ without options)

tux@freelinux:/usr/local$ cd ~
tux@freelinux:~$ pwd
/home/tux

cd .. –> change to parent directory (one directory up)

tux@freelinux:~$ pwd
/home/tux
tux@freelinux:~$ cd ..
tux@freelinux:/home$ pwd
/home

cd – –> change to previous working directory

tux@freelinux:~$ cd /var/www/html
tux@freelinux:/var/www/html$ cd –
/home/tux

 

4. su
-command to change user ID or become superuser. su short for switch user

Useful options with ‘su’ command

su [username] –> switch to username, if no username , it will default to root

tux@freelinux:~$ whoami
tux
tux@freelinux:~$ su darwin
Password:
darwin@freelinux:/home/tux$ su
Password:
root@freelinux:/home/tux#

su – –> if added with hypen, it will provide an environment similar to what the user would expect had the user logged in directly. Compare below the current directory,with and without the “-” . You can also use “-l” or ‘–login’

tux@freelinux:~$ su darwin
Password:
darwin@freelinux:/home/tux$ pwd
/home/tux
darwin@freelinux:/home/tux$ exit
exit
tux@freelinux:~$ su – darwin
Password:
darwin@freelinux:~$ pwd
/home/darwin

su -s –> use ‘-s’ to specify the shell environment

darwin@freelinux:~$ echo $SHELL
/bin/bash
darwin@freelinux:~$ su -s /bin/sh tux
Password:
$ echo $SHELL
/bin/sh
$ whoami
tux

 

5. passwd
-command to change user password.

Useful options with ‘passwd’ command

passwd [username] –> if no username is specified, it will reset password of logged-in user. The “root” user has the full privilege to change the password for any user, while normal user can only change his/her own account

tux@freelinux:~$ passwd
Changing password for tux.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
tux@freelinux:~$ passwd darwin
passwd: You may not view or modify password information for darwin.

root@freelinux:~# passwd darwin
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully


passwd -S [username]–> will show password status info of a user

tux@freelinux:~$ passwd -S
tux P 01/03/2021 0 99999 7 -1

“root” user command options

passwd -e [username]–> to set account’s password expiry immediately, which whill force a user to change his/her password at next user’s login

root@freelinux:~# passwd -S darwin
darwin P 01/03/2021 0 99999 7 -1
root@freelinux:~# passwd -e darwin
passwd: password expiry information changed.
root@freelinux:~# passwd -S darwin
darwin P 01/01/1970 0 99999 7 -1

WARNING: Your password has expired.
You must change your password now and login again!
Changing password for darwin.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

passwd -l –> lock the password of user

root@freelinux:~# passwd -l darwin
passwd: password expiry information changed.
root@freelinux:~# passwd -S darwin
darwin L 01/03/2021 0 99999 7 -1

passwd -u –> unlock user password

root@freelinux:~# passwd -S darwin
darwin L 01/03/2021 0 99999 7 -1
root@freelinux:~# passwd -u darwin
passwd: password expiry information changed.
root@freelinux:~# passwd -S darwin
darwin P 01/03/2021 0 99999 7 -1

passwd -w (value) –> set number of days of warning before a change of password is required

root@freelinux:~# passwd -S tux
tux P 01/03/2021 0 99999 7 -1
root@freelinux:~# passwd -w 2 tux
passwd: password expiry information changed.
root@freelinux:~# passwd -S tux
tux P 01/03/2021 0 99999 2 -1

passwd -n [value] –> set minimum number of days between password changes,e.g. set to 30 days

root@freelinux:~# passwd -S darwin
darwin P 01/03/2021 0 99999 7 -1
root@freelinux:~# passwd -n 30 darwin
passwd: password expiry information changed.
root@freelinux:~# passwd -S darwin
darwin P 01/03/2021 30 99999 7 -1

 

6. cp
– command to copy files and directories

Useful options with ‘cp’ command
cp {options} Source Destination

root@freelinux:/mnt# cp /etc/passwd /mnt/archives/
root@freelinux:/mnt# ls -l /mnt/archives/
total 4
-rw-r–r– 1 root root 1711 Jan 3 18:32 passwd

cp {options} Source Source Source Source Destination

root@freelinux:~#cp /var/log/wtmp /etc/shadow /etc/group /mnt/archives
root@freelinux:~# ls /mnt/archives
group passwd shadow wtmp

cp -i –> short for interactive, prompt before overwrite

root@freelinux:~# cp -i /etc/fstab /mnt/archives/
cp: overwrite ‘/mnt/archives/fstab’? y

cp -v –> verbose

root@freelinux:~# cp -v /etc/gshadow /mnt/archives/
‘/etc/gshadow’ -> ‘/mnt/archives/gshadow’

cp -r (or cp -R or cp –recursive) –> copy directories recursively

root@freelinux:~# cp -r /home/tux /mnt/archives/


7. rm
-command to remove files or directories
rm {file}
rm {file1} {file2}

tux@freelinux:~$ ls -l
total 0
-rw-rw-r– 1 tux tux 0 Jan 4 00:30 testfile1
-rw-rw-r– 1 tux tux 0 Jan 4 00:30 testfile2
-rw-rw-r– 1 tux tux 0 Jan 4 00:30 testfile3
tux@freelinux:~$ rm testfile1
tux@freelinux:~$ rm testfile2 testfile3
tux@freelinux:~$ ls -l
total 0

Useful options with ‘rm’ command
rm -i –> will prompt before every removal

tux@freelinux:~$ ls -l
total 0
-rw-rw-r– 1 tux tux 0 Jan 4 00:31 file1
-rw-rw-r– 1 tux tux 0 Jan 4 00:31 file2
tux@freelinux:~$ rm -i file1
rm: remove regular empty file ‘file1’? y
tux@freelinux:~$ ls -l
total 0
-rw-rw-r– 1 tux tux 0 Jan 4 00:31 file2

rm -r –> recursively remove directories and contents

tux@freelinux:~$ ls -R folder1/
folder1/:
folder2

folder1/folder2:
folder3

folder1/folder2/folder3:
tux@freelinux:~$ rm folder1/
rm: cannot remove ‘folder1/’: Is a directory
tux@freelinux:~$ rm -r folder1/
tux@freelinux:~$ ls -l
total 0


rm -f –> force remove, will ignore nonexistent files, never prompt 

tux@freelinux:~$ rm testfile1
rm: remove write-protected regular empty file ‘testfile1’? n
tux@freelinux:~$ rm -f testfile1

8. mv
-command to move or rename files
mv [filename/dir] [new_filename/new_dir] –> rename
mv [source][destination] –> move

tux@freelinux:~$ ls -l
total 0
-rw-rw-r– 1 tux tux 0 Jan 4 00:41 file111
tux@freelinux:~$ mv file111 file222
tux@freelinux:~$ ls -l
total 0
-rw-rw-r– 1 tux tux 0 Jan 4 00:41 file222

tux@freelinux:~$ ls -R folder1/
folder1/:
folder2 testfile1

folder1/folder2:
folder3

folder1/folder2/folder3:
tux@freelinux:~$ mv /home/tux/folder1/testfile1 /home/tux/folder1/folder2/
tux@freelinux:~$ ls -R folder1/
folder1/:
folder2

folder1/folder2:
folder3 testfile1

folder1/folder2/folder3:

Useful options with ‘mv’ command
mv -i –> prompt before overwrite

tux@freelinux:~$ mv -i /home/tux/dir1/file1 /home/tux/dir1/dir2
mv: overwrite ‘/home/tux/dir1/dir2/file1’? n


mv -f -> force move, will not prompt when overwriting

tux@freelinux:~$ mv -f /home/tux/dir1/file1 /home/tux/dir1/dir2

 


9. find
-command to search files in a directory hierarchy

Useful options with ‘find’ command

find / -name [filename] –> find all the files starting from the root directory (/) with  name “filename” 

tux@freelinux:~$ find / -name filename1
find: /mnt/archives/tux/.cache: Permission denied
find: /home/darwin/.cache: Permission denied
/home/tux/filename1

find -type d –> find in the specific path and look only for directories

darwin@freelinux:~$ ls -l
total 8
drwxrwxr-x 2 darwin darwin 4096 Jan 4 02:31 dir1
drwxrwxr-x 2 darwin darwin 4096 Jan 4 02:31 dir2
-rw-rw-r– 1 darwin darwin 0 Jan 4 02:31 filename1
-rw-rw-r– 1 darwin darwin 0 Jan 4 02:32 filename2
darwin@freelinux:~$ find . -type d
.
./dir2
./dir1
./.cache

find -type f–> find in the specific path and look only for files

darwin@freelinux:~$ ls -l
total 8
drwxrwxr-x 2 darwin darwin 4096 Jan 4 02:31 dir1
drwxrwxr-x 2 darwin darwin 4096 Jan 4 02:31 dir2
-rw-rw-r– 1 darwin darwin 0 Jan 4 02:31 filename1
-rw-rw-r– 1 darwin darwin 0 Jan 4 02:32 filename2
darwin@freelinux:~$ find . -type f
./.profile
./.bashrc
./.bash_history
./filename2
./.bash_logout
./filename1
./.cache/motd.legal-displayed

find . -type -f -perm xxx –>  find files with specified permission

darwin@freelinux:~$ find . -type f -perm 775
./filename2

find -size[+/-] –> find files less or greater than specified size
e.g. find files larger than 100Mb

tux@freelinux:~$ sudo find / -type f -size +100M
/proc/kcore
tux@freelinux:~$ ls -l /proc/kcore
-r——– 1 root root 140737477881856 Jan 4 02:47 /proc/kcore
tux@freelinux:~$ ls -lh /proc/kcore
-r——– 1 root root 128T Jan 4 02:47 /proc/kcore

 

10. grep
-command to print lines matching a pattern
Syntax:
grep “word to search” filename
grep “string1 string2 string3” filename
command | grep “word”

tux@freelinux:~$ grep darwin /etc/passwd
darwin:x:1001:1001:,,,:/home/darwin:/bin/bash

Note: you can use -r to search recursively, or use -i to ignore case

tux@freelinux:~$ cat /etc/passwd | grep darwin
darwin:x:1001:1001:,,,:/home/darwin:/bin/bash

grep – w –> to select only containing matches or word-regexp
See the difference between the two examples: (will only select the file with the exact matching

darwin@freelinux:~$ cat filename1
coffee123
darwin@freelinux:~$ cat filename2
coffee
darwin@freelinux:~$ grep -w “coffee” filename*
filename2:coffee

grep -c –> to count number of matches

darwin@freelinux:~$ grep -c “coffee” filename*
filename1:1
filename2:1

grep -c –> count number of matches

tux@freelinux:/var/log$ grep -c “user” /var/log/auth.log
310

grep -r –> to read all files recursively under directory
grep -i –> print only matching and ignore case

About the author

Free Linux Tutorials

View all posts

Leave a Reply