ADVERTISEMENT

Linux Questions

The How to guide for Linux Commands

  • uname -s
    print the kernel name.

  • uname -n
    print the network node hostname.

  • uname -r
    print the kernel release.

  • uname -v
    print the kernel version.

  • uname -m
    print the machine hardware name.

  • uname -p
    print the processor type

  • uname -i
    print the hardware platform.

  • uname -o
    print the operating system.

  • 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

  • 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

  • who am i
    print who you are logged in as. same as who -m

  • 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 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 -l
    print the entries in the long list format

  • 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 -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 -R
    List the files and directories in current directory and recursive to the child directories

  • ls -S -l
    list the files and directories sorted in the descending size order

  • ls -r -Sl
    list the files and directories sorted in the ascending size order

  • 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 -h -l
    list the files and directories with human readable sizes

  • ls -s
    list the files and directories with the allocated space for each

  • ls -U
    list entries in directory order without any sorting

  • ls -X
    list the files and directories sorted alphabetically by extension

  • 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 -f *
    deletes all the files in the current directory without any prompt

  • 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 -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 -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 -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

  • 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 -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 -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 -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 -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.

  • 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 -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.

  • 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.

  • 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

  • cp my_file1 my_file2
    copy my_file1 as my_file2. if my_file2 already exists , it will be overwritten.

  • 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 -n my_file1 my_file2
    copy my_file1 as my_file2.The file will not be copied if my_file2 already exists.

  • 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 -l my_file1 my_file2
    create a hard link of my_file1 as my_file2

  • 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

  • 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 -p my_file1 my_file2
    copy my_file1 as my_file2 along with the attributes such as time stamp, mode etc.

  • 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 -n my_file1
    display the contents of file my_file1 with the line numbers

  • 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 -b my_file1
    display the contents of my_file1 with each non-empty lines numbered

  • 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

  • cat -E my_file1
    display the contents of my_file1 with $ at end of each line

  • cat my_file1 | sort > my_file2
    sort the contents of my_file1 and create a new file my_file2 with that content

  • head my_file1
    display the first 10 lines from my_file1

  • head -n 2 my_file1
    display the first 2 lines from my_file1

  • head my_file1 my_file2
    display the first 10 lines from my_file1 and my_file2

  • head -v my_file1 my_file2
    display the first 10 lines from my_file1 and my_file2 separated by respective filenames header

  • head -q my_file1 my_file2
    display the first 10 lines from my_file1 and my_file2 without any header information

  • head -n -2 my_file1
    display the contents of my_file1 except the last 2 lines.

  • 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 5 my_file1 > my_file2
    create a file called my_file2 with first 5 lines from my_file1

  • tail my_file1
    print the last 10 lines from my_file1

  • 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 -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 -q my_file1 my_file2
    quietly display the last 10 lines from my_file1 and my_file2 without any header information

  • 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 -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

  • rmdir my_dir1
    delete empty directory called my_dir1. it will throw an error if the directory not empty

  • 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

  • rmdir -p my_dir1/my_dir2/my_dir3
    delete my_dir3 first, my_dir2 on second and then my_dir1

  • rmdir my_dir1 my_dir2 my_dir3
    delete my_dir1, my_dir2 and my_dir3 at once

  • 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 -P linkto_my_dir1
    change the current directory to my_dir1 where linkto_mydir1 points to

  • 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

  • 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.

  • unlink my_file1_link1
    unlink my_file1_link1

  • ls -i -l
    list contents of current directory with the index number(inode) for each files and sub directories

  • 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 -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 -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 -t my_dir1 my_file1
    create a hard link called my_file1 which refers to my_file1 in my_dir1 directory

  • ln my_dir1/* my_dir2
    create hard links for all the files under my_dir1 in directory my_dir2

  • 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 -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.

  • ls -d -l */
    list only the directories under the current directory

  • 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

  • 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 -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 -t 2M -mh my_dir1
    display all the files and subdirectories under my_dir1 whose disk usage is more than 2MB

  • 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 -d 1 -h
    display the disk usage summary of all the directories in current directory

  • du -h * | sort -h
    display the disk usage summary of all the directories and subdirectories in current directory sorted by the size

  • df
    display all file systems and their disk usage

  • 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 -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

  • 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 -T /boot
    display disk usage of boot filesystem

  • 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 -n this is a text
    display - this is a text - with OUT a trailing new line.

  • echo "my sentence"
    display - my sentence( will not interpret escape characters inside the quotes on default.)

  • 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 *
    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 -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

  • 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.

  • 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.

  • 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 -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.

  • 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.

  • touch -h my_file1_link1
    update the times of symbolic link my_file1_link1, not the file which is pointing to.

  • 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 -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 -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

  • ifconfig
    Displays the status of the currently active network interfaces

  • 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.

  • 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 -f /dev/sda
    display the file system information of first hard disk

  • stat -c %x%y%z my_file1
    display only the access time(atime), Modification time(mtime) and Change time(ctime) of my_file1.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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 -f
    displays information about the users currently on the machine and their processes with the from( remote host) information toggled.

  • 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

  • w user_1
    displays information specific to user_1 and its processes

  • 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.

  • touch my_file{1..5}
    create 5 empty files as names my_file1, my_file2, my_file3, my_file4 and my_file5

  • rm -f my_file{1..5}
    delete files my_file1, my_file2, my_file3, my_file4 and my_file5 without any prompting

  • mkdir user_{logs,commands}
    create 2 directories with names - user_logs and user_commands

  • echo `date`
    execute date command and display the output.

  • 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.

  • ifconfig -a
    Displays the status of all the active and inactive 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

  • 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.

  • unalias lll
    remove a previously defined alias "lll"

  • unalias -a
    remove all alias definitions