Linux / Unix Command Reference 1 File handling ls list files in current directory ls -l list files in current directory and show in long format to see permissions, size, and modification date ls -a list all files in current dir, including hidden files ls <dir-name> list all files in directory ls *.txt List all file which has txt extension in current dir ls -ltr List files in reverse time order pwd Show the path of current directory mkdir <dir-name> Create a directory touch <file-name> Create or update file cp <file1> <file2> Copy file1 to file2 cp –r <dir1> <dir2> Copy dir1 to dir2, create dir2 if it doesn’t exist mv <file1> <file2> Rename file1 to file2 rm <file-name> Delete file rm –r <dir-name> Delete directory rm –f <file-name> Forcefully delete file rm –rf <dir-name> Forcefully remove file cat <file-name> Output contain of file more <file-name> Output contents of file with page break head <file-name> Output the first 10 lines of file tail <file-name> Output the last 10 lines of file tail –f <file-name> Output the last 10 lines of running file cd <dir-name> Change directory cd .. Go up one directory cd /opt Go to /opt directory cd ~ Go to your home directory cd - Go to the last directory you were just in ls -s <file> <link-file> Create a symbolic link to file echo “message” Display a line of message wc <file-name> Count no of lines, words and bytes of the file Compression tar cf <home.tar> <home> Create tar named home.tar containing home/ tar xf <home.tar> Extract the files/folder from home.tar tar czf <file.tar.gz> <files> Create a tar with gzip compression gzip <file> Compress file and renames it to file.gz tar -xzvf <file-name.tgz> Untar tgz file Search grep <pattern> <files> Search for pattern in files grep -r <pattern> <dir> Search recursively for pattern in directory locate <file-name> Find all instances of file find </> -name <“report”> Find file in root directory that start with “report” command | grep <pattern> Search for pattern in the output of cmd File permissions chmod ugo <file-name> Change permission of file to ugo u - user, g - group, o - other 4 - read, 2 – write, 1 - execute chmod 755 <file-name> You can read, write and execute and everyone else can read and execute chmod -R 600 <dir-name> Set permission to all files inside directory Piping & redirection > Redirect standard output (STDOUT) to a file >> Append standard output to a file < Pass the contents of the file to a program a STDIN | Feed the STDOUT of the program on the left as STDIN to the program on the right
Linux / Unix Command Reference 2 System man <command> Show the manual of command uname -a Display linux system information uptime Show how long the system is up hostname Show system host name date Show current date and time cal Show this month calendar whoami Who you are logged in as finger Display information about user df -h Show free space on mounted filesystems du -skh Display disk usage cat /proc/cpuinfo CPU information cat /proc/meminfo Memory information shutdown –h now Shut down machine reboot restart machine whereis <app> Show possible location of app which <app> Show which app will be run by default script <file-name> Makes a typescript Networking ifconfig –a Show all network interface information iwconfig Show wireless interface information Ifconfig interface <ipaddr> Configure the ipv4 address mii-tool <eth0> Show eth0 status ping <ipaddr> Test connectivity between hosts route add default gw <ip> Set default route tcpdump -n -i <eth0> Display packet information on eth0 wget <file-name> Download file ssh <user@host> Login to remote host as a user scp <file> <user@host:/tmp> Secure copy a file from your machine to tmp directory on a remote server scp -r <dir> <user@host:~> Secure copy a dir to home dir of remote sys. sudo iwlist scan Scan for wireless network /etc/network/interfaces Manual configuration of interface nslookup <domain-name> Find domain name to IP address Process Management ps Display your currently active processes top Display all running processes kill <pid> Kill process id pid kill -9 <pid> Force kill process pid killall <pid> Kill process pid ps | grep <process-name> Get information about particular process Installation ./configure make make install Install package from source code apt-get install <pkg-name> Install a package in ubuntu from internet apt-get remove <pkg-name> Uninstall package in ubuntu apt-get purge <pkg-name> Uninstall package in ubuntu apt-get upgrade Upgrade all packages in ubuntu apt-get update Refresh available updates dpkg -i <pkg.deb> Install debian package in ubuntu rpm -ivh <pkg.rpm> Install rpm package in fedora rpm -ev <pkg-name> Uninstall package in fedora/redhat yum install <pkg-name> Install a package in fedora from Internet
Linux / Unix Command Reference 3 vi editor vi <file> Open/Create file vi +50 <file> Open file from 50th line i Insert mode Esc Return to Command mode :w Save file :q Exit from file :wq Save and exit from file h Cursor left j Cursor down k Cursor up l Cursor right yy Copy a line 3yy Copy 3 lines p Paste line dd Delete line 3dd Delete 3 lines u Undo last change r Replace character x Delete character gg Start of document G Last line of the file :n nth line of file $ End of the line ^ Beginning of the line /search-text Search text from top to bottom ?search-text Search text from bottom to top :%s/pattern/text/g Replace all occurrences of pattern with text Ubuntu Reference sudo <command> Run command as a root sudo su Open a root shell service ssh start Start ssh service service ssh stop Stop ssh service service ssh status Show ssh service status /etc/init.d/networking restart Restart network User Administration adduser username Create new user passwd username Set password Advanced Filters - sed sed -i ‘s/s1/s2/g’ hello.txt Replace s1 with s2 in hello.txt file sed –n ‘2,5 p’ hello.txt Print line 2 to 5th line of hello.txt file sed ‘2,5 d’ hello.txt Delete 2nd to 5th line of hello.txt file sed ‘2 a new-line’ hello.txt Append new-line after 2nd line of hello.txt Advanced Filters - awk awk ‘{print $2}’ hello.txt Print 2nd column of hello.txt awk -F”,” ‘{print $2}’ hello.txt Print 2nd column of hello.txt where delimiter is “,” awk '/prof/ { print $3 }' hello.txt If line contains “prof” then print 3rd column of hello.txt awk -F”|” ‘$1 > 7500 { print $2,$3} ‘ hello.txt If 1st column value more 7500 then print column 2nd and 3rd of hello.txt

Unix / Linux Command Reference

  • 1.
    Linux / UnixCommand Reference 1 File handling ls list files in current directory ls -l list files in current directory and show in long format to see permissions, size, and modification date ls -a list all files in current dir, including hidden files ls <dir-name> list all files in directory ls *.txt List all file which has txt extension in current dir ls -ltr List files in reverse time order pwd Show the path of current directory mkdir <dir-name> Create a directory touch <file-name> Create or update file cp <file1> <file2> Copy file1 to file2 cp –r <dir1> <dir2> Copy dir1 to dir2, create dir2 if it doesn’t exist mv <file1> <file2> Rename file1 to file2 rm <file-name> Delete file rm –r <dir-name> Delete directory rm –f <file-name> Forcefully delete file rm –rf <dir-name> Forcefully remove file cat <file-name> Output contain of file more <file-name> Output contents of file with page break head <file-name> Output the first 10 lines of file tail <file-name> Output the last 10 lines of file tail –f <file-name> Output the last 10 lines of running file cd <dir-name> Change directory cd .. Go up one directory cd /opt Go to /opt directory cd ~ Go to your home directory cd - Go to the last directory you were just in ls -s <file> <link-file> Create a symbolic link to file echo “message” Display a line of message wc <file-name> Count no of lines, words and bytes of the file Compression tar cf <home.tar> <home> Create tar named home.tar containing home/ tar xf <home.tar> Extract the files/folder from home.tar tar czf <file.tar.gz> <files> Create a tar with gzip compression gzip <file> Compress file and renames it to file.gz tar -xzvf <file-name.tgz> Untar tgz file Search grep <pattern> <files> Search for pattern in files grep -r <pattern> <dir> Search recursively for pattern in directory locate <file-name> Find all instances of file find </> -name <“report”> Find file in root directory that start with “report” command | grep <pattern> Search for pattern in the output of cmd File permissions chmod ugo <file-name> Change permission of file to ugo u - user, g - group, o - other 4 - read, 2 – write, 1 - execute chmod 755 <file-name> You can read, write and execute and everyone else can read and execute chmod -R 600 <dir-name> Set permission to all files inside directory Piping & redirection > Redirect standard output (STDOUT) to a file >> Append standard output to a file < Pass the contents of the file to a program a STDIN | Feed the STDOUT of the program on the left as STDIN to the program on the right
  • 2.
    Linux / UnixCommand Reference 2 System man <command> Show the manual of command uname -a Display linux system information uptime Show how long the system is up hostname Show system host name date Show current date and time cal Show this month calendar whoami Who you are logged in as finger Display information about user df -h Show free space on mounted filesystems du -skh Display disk usage cat /proc/cpuinfo CPU information cat /proc/meminfo Memory information shutdown –h now Shut down machine reboot restart machine whereis <app> Show possible location of app which <app> Show which app will be run by default script <file-name> Makes a typescript Networking ifconfig –a Show all network interface information iwconfig Show wireless interface information Ifconfig interface <ipaddr> Configure the ipv4 address mii-tool <eth0> Show eth0 status ping <ipaddr> Test connectivity between hosts route add default gw <ip> Set default route tcpdump -n -i <eth0> Display packet information on eth0 wget <file-name> Download file ssh <user@host> Login to remote host as a user scp <file> <user@host:/tmp> Secure copy a file from your machine to tmp directory on a remote server scp -r <dir> <user@host:~> Secure copy a dir to home dir of remote sys. sudo iwlist scan Scan for wireless network /etc/network/interfaces Manual configuration of interface nslookup <domain-name> Find domain name to IP address Process Management ps Display your currently active processes top Display all running processes kill <pid> Kill process id pid kill -9 <pid> Force kill process pid killall <pid> Kill process pid ps | grep <process-name> Get information about particular process Installation ./configure make make install Install package from source code apt-get install <pkg-name> Install a package in ubuntu from internet apt-get remove <pkg-name> Uninstall package in ubuntu apt-get purge <pkg-name> Uninstall package in ubuntu apt-get upgrade Upgrade all packages in ubuntu apt-get update Refresh available updates dpkg -i <pkg.deb> Install debian package in ubuntu rpm -ivh <pkg.rpm> Install rpm package in fedora rpm -ev <pkg-name> Uninstall package in fedora/redhat yum install <pkg-name> Install a package in fedora from Internet
  • 3.
    Linux / UnixCommand Reference 3 vi editor vi <file> Open/Create file vi +50 <file> Open file from 50th line i Insert mode Esc Return to Command mode :w Save file :q Exit from file :wq Save and exit from file h Cursor left j Cursor down k Cursor up l Cursor right yy Copy a line 3yy Copy 3 lines p Paste line dd Delete line 3dd Delete 3 lines u Undo last change r Replace character x Delete character gg Start of document G Last line of the file :n nth line of file $ End of the line ^ Beginning of the line /search-text Search text from top to bottom ?search-text Search text from bottom to top :%s/pattern/text/g Replace all occurrences of pattern with text Ubuntu Reference sudo <command> Run command as a root sudo su Open a root shell service ssh start Start ssh service service ssh stop Stop ssh service service ssh status Show ssh service status /etc/init.d/networking restart Restart network User Administration adduser username Create new user passwd username Set password Advanced Filters - sed sed -i ‘s/s1/s2/g’ hello.txt Replace s1 with s2 in hello.txt file sed –n ‘2,5 p’ hello.txt Print line 2 to 5th line of hello.txt file sed ‘2,5 d’ hello.txt Delete 2nd to 5th line of hello.txt file sed ‘2 a new-line’ hello.txt Append new-line after 2nd line of hello.txt Advanced Filters - awk awk ‘{print $2}’ hello.txt Print 2nd column of hello.txt awk -F”,” ‘{print $2}’ hello.txt Print 2nd column of hello.txt where delimiter is “,” awk '/prof/ { print $3 }' hello.txt If line contains “prof” then print 3rd column of hello.txt awk -F”|” ‘$1 > 7500 { print $2,$3} ‘ hello.txt If 1st column value more 7500 then print column 2nd and 3rd of hello.txt