ADVERTISEMENT

Linux Commands

A searchable database of linux command examples
alias - Define or display aliases.
Syntax:
alias [-p] [name[=value] ... ]
  • alias
    prints the list of aliases in the reusable format "alias NAME=VALUE" on standard output.
  • alias lll='ls -la'
    define a new alias "lll" for ls -la command
  • alias -p
    prints the list of aliases in the reusable format "alias NAME=VALUE" on standard output.
cat - Concatenate FILE(s), or standard input, to standard output.
Syntax:
cat [OPTION]... [FILE]...
  • cat my_file1
    display the contents of my_file1
  • cat my_file1 my_file2
    display the contents of my_file1 and my_file2
  • cat > my_file1
    awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ā€˜dā€˜) to exit. The text will be written in my_file1 file.
  • cat my_file1 > my_file2
    redirects the contents of my_file1 to create my_file2. if my_file2 already exists, its contents will be over written.
  • cat my_file1 >> my_file3
    append the contents of my_file3 with my_file1. if my_file3 does not exist, a new file will be created with contents of my_file1
  • cat my_file1 my_file2 my_file3 > my_file4
    concatenate my_file1, my_file2, my_file3 and save as my_file4
  • cat < my_file1
    display the contents of my_file1
  • cat my_file1 | sort > my_file2
    sort the contents of my_file1 and create a new file my_file2 with that content
  • cat -b my_file1
    display the contents of my_file1 with each non-empty lines numbered
  • cat -E my_file1
    display the contents of my_file1 with $ at end of each line
  • cat -n my_file1
    display the contents of file my_file1 with the line numbers
  • cat -s my_file1
    display the contents of my_file1. multiple blank lines coming adjacent to each other will combined to a single blank line
  • cat -T my_file1
    display the contents of my_file1. the tabs are indicated by ^I
cd - Change the current directory to dir
Syntax:
cd [-L|[-P [-e]]] [dir]
  • cd
    change the current directory to HOME directory
  • cd ~
    change the current directory to HOME directory
  • cd -
    change the current directory to OLDPWD( previous working directory)
  • cd ..
    change the current directory to the parent directory
  • cd -L linkto_my_dir1
    change the current directory to linkto_my_dir1 instead to the actual directory my_dir1 to where linkto_mydir1 pointing
  • cd -P linkto_my_dir1
    change the current directory to my_dir1 where linkto_mydir1 points to
chmod - Change file mode bits
Syntax:
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

In simplified term

chmod [OPTION] [PERMISSIONS] filename
  • chmod o=r my_file1
    set the permission to access the file my_file1 to read-only for other users. Permission for owner and groups will be unchanged.
  • chmod 764 my_file1
    Set the permission to my_file1 as below.

    -the user can read, write, and execute it.
    -members of the user group can read and execute it.
    -others may only read it.
  • chmod u=rwx,g=rx,o=r my_file1
    Set the permission to my_file1 as below.

    -the user can read, write, and execute it.
    -members of the user group can read and execute it.
    -others may only read it.
  • chmod -f 764 my_file1
    Set the permission to my_file1 as below and suppress any error message(Quiet Mode )

    -the user can read, write, and execute it.
    -members of the user group can read and execute it.
    -others may only read it.
  • chmod -R 764 my_dir1
    Set the below permission to my_dir1 and all the files and directories under it recursively.

    -the user can read, write, and execute it.
    -members of the user group can read and execute it.
    -others may only read it.
  • chmod -v 764 my_file1
    Set the permission to my_file1 as below and shows a diagnostic message( Verbose Mode)

    -the user can read, write, and execute it.
    -members of the user group can read and execute it.
    -others may only read it.
cp - Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Syntax:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
  • cp my_file1 my_file2
    copy my_file1 as my_file2. if my_file2 already exists , it will be overwritten.
  • cp -f my_file1 my_file2
    copy my_file1 as my_file2. any existing file called my_file2 will be overwritten without any prompting.
  • cp -i my_file1 my_file2
    copy my_file1 as my_file2. if my_file2 already exists , there will be a prompt before overwriting the file.
  • cp -l my_file1 my_file2
    create a hard link of my_file1 as my_file2
  • cp -n my_file1 my_file2
    copy my_file1 as my_file2.The file will not be copied if my_file2 already exists.
  • cp -p my_file1 my_file2
    copy my_file1 as my_file2 along with the attributes such as time stamp, mode etc.
  • cp -r my_dir1 home_dir1
    recursively copy my_dir1 and all files and directories under that to the home_dir1
  • cp -R my_dir1 home_dir2
    recursively copy my_dir1 and all files and directories under that to the home_dir2
  • cp -s my_file1 my_file2
    create a symbolic link of my_file1 as my_file2
  • cp -u my_file1 my_file2
    copy my_file1 as my_file2 if my_file2 not exists already. update the contents of my_file2 with my_file1 if the latter is newer than my_file2
df - Show information about the file system on which each FILE resides, or all file systems by default.
Syntax:
df [OPTION]... [FILE]...
  • df
    display all file systems and their disk usage
  • df -a
    display all file systems including dummy filesystems and their disk usage
  • df -B G
    display all file systems and their disk usage in GigaBytes
  • df -B M
    display all file systems and their disk usage in MegaBytes
  • df -B K
    display all file systems and their disk usage in KiloBytes
  • df -h
    display all file systems and their disk usage in human readable sizes( e.g., 1K 234M 2G)
  • df -H
    display all file systems and their disk usage in human readable sizes, uses the powers of 1000 instead of 1024
  • df -h -T /boot
    display disk usage of boot filesystem
  • df -i
    display all file systems and number of inodes used for each
  • df -T
    display all file systems and their disk usage along with the type of each file system
  • df -t xfs -T
    display disk usage of all the xfs filesystems
  • df -x xfs -T
    display disk usage of all the filesystems excluding xfs
du - Summarize disk usage of each FILE, recursively for directories.
Syntax:
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
  • du
    display the disk usage summary of all the directories and each of its sub directories
  • du my_dir1
    display the disk usage summary of all the directories under my_dir1 and each of its sub directories
  • du -a my_dir1
    display the disk usage summary of all the files and directories under my_dir1 and each of its sub directories
  • du -d 1 -h
    display the disk usage summary of all the directories in current directory
  • du -h my_dir1
    display the disk usage summary of all the directories under my_dir1 and each of its sub directories in a human readable format. the sizes are printed in KB,MB or GB respectively.
  • du -h * | sort -h
    display the disk usage summary of all the directories and subdirectories in current directory sorted by the size
  • du -k my_dir1
    display the summary of disk usage of directory tree along with its subtrees in Kilobytes
  • du -m my_dir1
    display the summary of disk usage of directory tree along with its subtrees in Megabytes
  • du -s -h
    display the total usage of current directory in human readable format
  • du -s -h my_dir1
    display the total usage of my_dir1 directory in human readable format
  • du -s -h *
    display the disk usage summary of all the files and directories of current directory not its sub directories.
  • du -s -h */
    display the disk usage summary of all the directories( not files ) in current directory
  • du -t 2M -mh my_dir1
    display all the files and subdirectories under my_dir1 whose disk usage is more than 2MB
echo - display a line of text
Syntax:
echo [OPTION]... [STRING]...
  • echo this is a text
    display - this is a text - with a trailing new line.
  • echo "this is a text"
    display - this is a text - with a trailing new line.
  • echo "my sentence"
    display - my sentence( will not interpret escape characters inside the quotes on default.)
  • echo *
    list all the files and directories in the current directory
  • echo *.txt
    list all the files and directories with an txt extension in the current directory
  • echo */
    list ONLY the directories in the current directory
  • echo $PWD
    display the current working directory
  • echo $PATH
    display the currently defined PATH
  • echo ~
    display the home directory
  • echo "this is a text" > my_file1
    create my_file1 with content "this is a text". if my_file1 already exists, its contents will be overwritten.
  • echo "this is a text" >> my_file1
    append the contents of my_file1 with "this is a text". if my_file1 does not exist, a new file will be created with the same text.
  • echo `date`
    execute date command and display the output.
  • echo -E "my sentence"
    display - my sentence.( will not interpret escape characters inside the quotes.)
  • echo -e "my sentence"
    display - my sentence( will interpret escape characters inside the quotes, in this case a TAB .)
  • echo -e "\"
    display \
  • echo -e "a"
    alert(BEL)
  • echo -e "\b"
    display backspace
  • echo -e "\n"
    display new line
  • echo -e " "
    display carriage return(enter)
  • echo -e "\t"
    display TAB
  • echo -n this is a text
    display - this is a text - with OUT a trailing new line.
head - Print the first 10 lines of each FILE to standard output.
Syntax:
head [OPTION]... [FILE]...
  • head my_file1
    display the first 10 lines from my_file1
  • head my_file1 my_file2
    display the first 10 lines from my_file1 and my_file2
  • head -c 100 my_file1
    display the first 100Bytes of my_file1
  • head -c -100 my_file1
    display the contents of my_file1 except the last 100Bytes
  • head -n 2 my_file1
    display the first 2 lines from my_file1
  • head -n -2 my_file1
    display the contents of my_file1 except the last 2 lines.
  • head -n 5 my_file1 > my_file2
    create a file called my_file2 with first 5 lines from my_file1
  • head -q my_file1 my_file2
    display the first 10 lines from my_file1 and my_file2 without any header information
  • head -v my_file1 my_file2
    display the first 10 lines from my_file1 and my_file2 separated by respective filenames header
ifconfig - Configure a Network Interface
Syntax:
ifconfig [-v] [-a] [-s] [interface]
ifconfig [-v] interface [aftype] options | address ...
  • ifconfig
    Displays the status of the currently active network interfaces
  • ifconfig eth0
    Displays the status of network interface eth0
  • ifconfig eth0 up
    activate the network interface eth0
  • ifconfig eth0 down
    deactivate the network interface eth0
  • ifconfig eth0 192.168.0.100
    Configure the network interface eth0 to use the static IP address 192.168.0.100.
  • ifconfig eth0 netmask 255.255.255.0
    Configure the network interface eth0 to use the network mask address 255.255.255.0
  • ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
    Configure the network interface eth0 to use the static IP address 192.168.0.100, 255.255.255.0 as network mask and 192.168.1.255 as broadcast address
  • ifconfig eth0 mtu 1500
    set the Maximum Transfer Unit(MTU) to 1500 of network interface eth0
  • ifconfig eth0 hw ether 08:00:27:29:17:bb
    change the MAC( hardware address ) of the network interface eth0
  • ifconfig -a
    Displays the status of all the active and inactive network interfaces
link - Call the link function to create a link named FILE2 to an existing FILE1
Syntax:
link FILE1 FILE2
  • link my_file1 my_file1_link1
    create a hard link called my_file1_link1 which refers to my_file1. the inode number will be same for both.
ln - Make links between files
Syntax:
ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...
  • ln my_file1 my_file1_link1
    create a hard link called my_file1_link1 which refers to my_file1. the inode number will be same for both.
  • ln my_dir1/* my_dir2
    create hard links for all the files under my_dir1 in directory my_dir2
  • ln -f my_file1 my_file1_link1
    create a hard link called my_file1_link1 which refers to my_file1. any existing file called my_file1_link1 will be overwritten
  • ln -i my_file1 my_file1_link1
    create a hard link called my_file1_link1 which refers to my_file1.prompted to overwrite any existing file called my_file1_link1
  • ln -s my_file1 my_file1_link1
    create a symbolic/soft link called my_file1_link1 which refers to my_file1. the inode number of the soft link defers the actual file.
  • ln -s my_dir1 my_dir1_link1
    create a symbolic/soft link called my_dir1_link1 which refers to directory my_dir1. the inode number of the soft link defers the actual directory.
  • ln -t my_dir1 my_file1
    create a hard link called my_file1 which refers to my_file1 in my_dir1 directory
ls - List information about the files and directories.
Syntax:
ls [OPTION]... [FILE]...
  • ls my_dir*
    list the contents of the directories starting with my_dir
  • ls my_file*
    list all the files starting with my_file in the current directory
  • ls -a
    print all the files and directories including the hidden ones starting with dot(.)
  • ls -A
    print all the files and directories including the hidden ones starting with dot(.) except the entries denoting the current directory(.) and parent directory(..)
  • ls -C
    list directory contents in a column format
  • ls -d my_dir*
    list all the directories starting with my_dir but not their contents.
  • ls -d -l */
    list only the directories under the current directory
  • ls -g
    print the entries in the long list format without owner information
  • ls -G -g
    print the entries in the long list format without owner and group information
  • ls -h -l
    list the files and directories with human readable sizes
  • ls -i -l
    list contents of current directory with the index number(inode) for each files and sub directories
  • ls -l
    print the entries in the long list format
  • ls -m
    print the entries in the comma separated format
  • ls -o
    print the entries in the long list format without group information
  • ls -p
    append / indicator to directories
  • ls -p -l | grep -v /
    list only the files under the current directory
  • ls -p -l | grep /
    list only the directories under the current directory
  • ls -R
    List the files and directories in current directory and recursive to the child directories
  • ls -r -Sl
    list the files and directories sorted in the ascending size order
  • ls -S -l
    list the files and directories sorted in the descending size order
  • ls -s
    list the files and directories with the allocated space for each
  • ls -t
    list the files and directories sorted by the modification time.newest appear first.
  • ls -t -r
    list the files and directories sorted by the modification time.oldest appear first.
  • ls -U
    list entries in directory order without any sorting
  • ls -X
    list the files and directories sorted alphabetically by extension
mkdir - Create the DIRECTORY(ies), if they do not already exist.
Syntax:
mkdir [OPTION]... DIRECTORY...
  • mkdir my_dir1
    create a directory called my_dir1 in the current directory
  • mkdir my_dir1 my_dir2
    create directories, my_dir1 and my_dir2 in the current directory
  • mkdir user_{logs,commands}
    create 2 directories with names - user_logs and user_commands
  • mkdir -p my_dir1/my_dir2
    create my_dir2 in my_dir1 if my_dir1 already exists. otherwise create my_dir1 and then my_dir2 under that.
  • mkdir -v -p my_dir1/my_dir2/my_dir3
    create the directory structure my_dir1/my_dir2/my_dir3 and print messages to indicate each activity
mv - Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Syntax:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
  • mv my_file1 my_dir1
    move my_file1 from the current directory to the directory my_dir1. If there is a file called my_file1 already exists in my_dir1, it will prompt for overwriting the same.
  • mv my_dir1 my_dir2
    move my_dir1 from the current directory to and existing directory my_dir2. my_dir1 will be renamed to my_dir2 if there is no existing directory called my_dir2
  • mv -f my_file1 my_dir1
    move my_file1 from the current directory to the directory my_dir1. If there is a file called my_file1 already exists in my_dir1, it will be overwritten without any prompting
  • mv -i my_file1 my_dir1
    move my_file1 from the current directory to the directory my_dir1. If there is a file called my_file1 already exists in my_dir1, it will prompt for overwriting the same.
  • mv -n my_file1 my_dir1
    move my_file1 from the current directory to the directory my_dir1. my_file1 will not be moved if there is a file called my_file1 already exists in my_dir1.
  • mv -t my_file1 my_dir1
    move my_file1 from the current directory to the directory my_dir1. If there is a file called my_file1 already exists in my_dir1, it will prompt for overwriting the same.
  • mv -T my_dir1 my_dir2
    rename my_dir1 to my_dir2. the contents of my_dir2 will be overwritten and renamed if it already exists
  • mv -u my_file1 my_dir1
    move my_file1 from the current directory to the directory my_dir1. the file will not be moved If there is a file called my_file1 already exists in my_dir1 newer than the source.
rm - Remove (unlink) the FILE(s).
Syntax:
rm [OPTION]... FILE...
  • rm -d *
    deletes all the empty directories in the current directory
  • rm -d -f *
    deletes all the empty directories in the current directory without any prompting
  • rm -f *
    deletes all the files in the current directory without any prompt
  • rm -f my_file{1..5}
    delete files my_file1, my_file2, my_file3, my_file4 and my_file5 without any prompting
  • rm -i *
    attempts to interactively remove all the files in the current directory.each file will be prompted and an affirmative response deletes the file.
  • rm -I *
    deletes all the files in the current directory without any prompting if the number of files are 3 or less. one time prompting to confirm if the files are more than 3.
  • rm -r my_dir
    deletes all the contents of my_dir and directory itself recursively.each entry will be prompted before deletion
  • rm -R my_dir
    deletes all the contents of my_dir and directory itself recursively.each entry will be prompted before deletion.
  • rm -R -f my_dir
    deletes all the contents of my_dir and directory itself recursively without any prompting
  • rm -r -f my_dir
    deletes all the contents of my_dir and directory itself recursively without any prompting.
  • rm -v -f *
    deletes all the files in the current directory without any prompting. the v option makes to display the file names that has been deleted
rmdir - Remove the DIRECTORY(ies), if they are empty.
Syntax:
rmdir [OPTION]... DIRECTORY...
  • rmdir my_dir1
    delete empty directory called my_dir1. it will throw an error if the directory not empty
  • rmdir my_dir1 my_dir2 my_dir3
    delete my_dir1, my_dir2 and my_dir3 at once
  • rmdir -p my_dir1/my_dir2/my_dir3
    delete my_dir3 first, my_dir2 on second and then my_dir1
  • rmdir -v my_dir1
    delete empty directory called my_dir1. it will throw an error if the directory not empty or else the directory will be removed with a message
stat - Display file or file system status.
Syntax:
stat [OPTION]... FILE...
  • stat *
    display the status information of all the files and directories in the current directory. the information includes size, file type, access permissions, inode number and access/change/modification time stamps
  • stat my_file1
    display the staus information of my_file1 in the current directory. the information includes size, file type, access permissions, inode number and access/change/modification time stamps
  • stat my_dir1
    display the staus information of my_dir1 in the current directory. the information includes size, file type, access permissions, inode number and access/change/modification time stamps
  • stat -c %x%y%z my_file1
    display only the access time(atime), Modification time(mtime) and Change time(ctime) of my_file1.
  • stat -c %n%x%y%z *
    display the name, access time(atime), Modification time(mtime) and Change time(ctime) of all the files and directories in current directory.
  • stat -f /dev/sda
    display the file system information of first hard disk
tail - Print the last 10 lines of each FILE to standard output.
Syntax:
tail [OPTION]... [FILE]...
  • tail my_file1
    print the last 10 lines from my_file1
  • tail -c 100 my_file1
    print the last 100Bytes from my_file1
  • tail -c +100 my_file1
    print the entire contents of my_file1 starting from 100th Byte
  • tail -f my_file1
    print the last 10 lines from my_file1, wait and update the display while any new lines added to the file.
  • tail -f -s 2 my_file1
    print the last 10 lines from my_file1, wait and update the display while any new lines added to the file in every 2 seconds
  • tail -n 5 my_file1
    print the last 5 lines from my_file1
  • tail -n +5 my_file1
    print the entire contents of my_file1 starting from 5th line
  • tail -q my_file1 my_file2
    quietly display the last 10 lines from my_file1 and my_file2 without any header information
touch - Update the access and modification times of each existing FILE to the current time or create new EMPTY files
Syntax:
touch [OPTION]... FILE...
  • touch my_file1
    if my_file1 already exists, set its access, modification, and change times (atime, mtime, and ctime) to the current system time. If my_file1 does not exist, create an empty file with that name with current system time.
  • touch my_file1 my_file2
    if my_file1 and my_file2 already exists, set its access, modification, and change times (atime, mtime, and ctime) to the current system time. If my_file1 and my_file2 are not existing, create empty files with that names with current system time.
  • touch *
    set the access, modification, and change times (atime, mtime, and ctime) of all the files and directories in the current directory to the current system time.
  • touch */
    set the access, modification, and change times (atime, mtime, and ctime) of all the directories(NOT FILES) in the current directory to the current system time.
  • touch my_file{1..5}
    create 5 empty files as names my_file1, my_file2, my_file3, my_file4 and my_file5
  • touch -a my_file1
    set the access time(atime) and Change time(ctime) of existing my_file1 to the current system time. Modification time(mtime) will not be changed. my_file1 will be created if it does not exist.
  • touch -c my_file1
    if my_file1 exists, set its access, modification, and change times (atime, mtime, and ctime) to the current system time. If my_file1 does not exist, do nothing.
  • touch -d "jan 2" my_file1
    set the access time(atime) and Change time(ctime) of existing my_file1 to JAN 2nd current year. Modification time(mtime) will not be changed. my_file1 will be created if it does not exist
  • touch -d "jan 2 2000" my_file1
    set the access time(atime) and Change time(ctime) of existing my_file1 to JAN 2nd 2000. Modification time(mtime) will not be changed. my_file1 will be created if it does not exist
  • touch -h my_file1_link1
    update the times of symbolic link my_file1_link1, not the file which is pointing to.
  • touch -r my_file1 my_file2
    set the access time(atime) and Change time(ctime) of existing my_file2 to match/reference with the times of my_file1 . Modification time(mtime) will not be changed. my_file2 will be created if it does not exist.
  • touch -t 201712011430.30 my_file1
    set the access time(atime) and Change time(ctime) of existing my_file1 to 2017 Dec 1st 14:30:30. Modification time(mtime) will not be changed. my_file1 will be created if it does not exist
unalias - Remove each NAME from the list of defined aliases.
Syntax:
unalias [-a] name [name ...]
  • unalias lll
    remove a previously defined alias "lll"
  • unalias -a
    remove all alias definitions
uname - Print system information
Syntax:
uname [OPTION]...
  • uname -a
    print the complete information on below order.( same as uname -snrvmpio )
    kernel name
    network node hostname
    kernel release
    kernel version
    machine hardware name
    processor type
    hardware platform
    operating system
  • uname -i
    print the hardware platform.
  • uname -m
    print the machine hardware name.
  • uname -n
    print the network node hostname.
  • uname -o
    print the operating system.
  • uname -p
    print the processor type
  • uname -r
    print the kernel release.
  • uname -s
    print the kernel name.
  • uname -v
    print the kernel version.
unlink - Call the unlink function to remove the specified FILE
Syntax:
unlink FILE
  • unlink my_file1_link1
    unlink my_file1_link1
w - Show who is logged on and what they are doing.
Syntax:
w [options] user [...]
  • w
    displays information about the users currently on the machine and their processes. the header section provides a summary of the system up time and number of users currently logged in.
  • w user_1
    displays information specific to user_1 and its processes
  • w -f
    displays information about the users currently on the machine and their processes with the from( remote host) information toggled.
  • w -h
    displays information about the users currently on the machine and their processes without header information which sums up the system uptime and number of users logged in etc.
  • w -i
    displays information about the users currently on the machine and their processes with remote host name as IP address format
  • w -s
    displays information about the users currently on the machine and their processes without PCPU(CPU time of the currently running process) and JCPU(Total CPU time used by the user since login ) information
who - Print information about users who are currently logged in.
Syntax:
who [OPTION]... [ FILE | ARG1 ARG2 ]
  • who am i
    print who you are logged in as. same as who -m
  • who -a
    same as who -bdlprtTu
  • who -b
    display the time of the last system boot.
  • who -d
    display dead processes.
  • who -H
    print line of column headings
  • who -l
    print system login processes
  • who -m
    list only hostname and user associated with stdin
  • who -p
    print active processes spawned by init
  • who -q
    list all login names and number of users logged on
  • who -r
    print current runlevel
  • who -s
    print only name, line, and time (default)
  • who -t
    print last system clock change
  • who -u
    list users logged in