Linux

Linux Commands cheat sheet for beginners and advance users.

Managing and using Linux is much more efficient than any other operating system when you know the right Linux commands. Our archive of Linux commands contains absolutely essential Linux commands and other critical commands required to batter manage things with Linux.

Linux Commands for basic file and directory operations

Print current working directory: pwd

#pwd 

Show files in current directory: ls

#ls

Show maximum information about all files, including hidden: ls -a

#ls -l

Move/rename a file or directory (be careful that you don’t move the source over a destination with the same name): mv [source] [destination]

#mv /home/file.txt /var/html

Delete target forever (be very careful), use -r recursive flag for directories: rm [target]

#rm file.txt

Copy file or directory: cp [source] [destination]

#cp /home/file.txt /var/html/

Mount filesytem(cdrom,usb): mount /dev/[device name] /media/[device name]

#mount /dev/cdrom /media/cdrom

Unmount the filesystem: umount /media/[device name]

#umount /media/cdrom

Forensically clone filesystems and do other low-level operations on files. Very dangerous:dd

#dd if=/dev/sda1 of=/dev/sdb1=/dev/sdb1 bs=64K conv=noerror, sync status=progress

Work with filesystems and partitions. (Easier, still quite dangerous): fdisk

#fdisk

Linux Commands for System Administration

Execute Linux commands as an administrator (dangerous, but necessary for system administration tasks):  sudo [command]

#sudo apt-get install nmap

Become system administrator: sudo -s

#sudo -s

Quit system administration(Exit root): exit

#exit

Check distro repositories for software updates(Debian based system):sudo apt-get update

#sudo apt-get update

Download and install updates (update first): sudo apt-get upgrade

#sudo apt-get upgrade

Search for package in the repositories: apt-cache search [keyword]

#apt-cache search nmap

Get more detail on one specific package: apt-cache show [package name]

#apt-cache show nmap

Download and install a package: sudo apt-get install [package name]

#sudo apt-get install nmap

View the output of a command in a more convenient format: [command] | less

# cat /var/log/messages | less

Install a .deb file from command line: sudo dpkg -i package.deb

#sudo dpkg -i nmap.deb

Linux Commands for working with files

Print contents of file in terminal: cat [file]

#cat /etc/apt/sources.list

Find files matching [filename]: locate [filename]

#locate httpd.conf

Search through [filename] for matches to [phrase]: grep [phrase] [filename]

#grep server httpd.conf

Search through output of a command for [phrase]: [command] | grep [phrase]

#lspci | grep VGA

View the differences between two files: diff [file 1] [file 2]

#diff /etc/apt/sources.list /etc/apt/sources.list.bkup

Output the top -n lines of [file]: head -n [number of lines] [file]

#head -n 6 /etc/apt/sources.list

Like head, but it outputs the last -n lines: tail

#tail -n 6 /etc/apt/sources.list 

Checksum a file: md5sum [file]

#md5sum package.iso

Checksum every file in a directory: md5deep [directory] 

#md5deep /home

Call [command] every -n seconds, and display output: watch -n [number of seconds] [command]

#watch -n 20 lsusb

Execute [command], print how long it took: time [command]

#time lsusb

Remove spaces from filenames in current directory: rename -n ‘s/[\s]/”/g’ *

#rename -n 's/[\s]/''/g' *

change capitals to lowercase in filenames in current directory: rename ‘y/A-Z/a-z/’ *

#rename 'y/A-Z/a-z/' * 

Working With Processes:

List all running processes: ps -e

#ps -e 

Standard system monitor showing a more extensive view of all processes and system resources: top

#top

Like top, but with a better, cleaner interface: htop

#htop

Stop a process from using all system resources and lagging computer: nice [process name]

#nice httpd

Kill misbehaving process (use sparingly, last resort, try ‘nice’ command first): pkill [process name]

pkill httpd

Compression and Encryption:

Make a simple compressed backup of a file or directory: tar -cvzf [backup output.tgz] [target file or directory]

#tar -cvzf etc.tgz /etc

Encrypt a file: gpg -o [outputfilename.gpg] -c [target file]

#gpg -o outputfilename.gpg -c targetfile

Decrypt a file: gpg -o [outputfilename] -d [target.gpg]

#gpg -o outputfilename -d target.gpg

Zip and encrypt a directory simultaneously: gpg-zip -o encrypted-filename.tgz.gpg -c -s file-to-be-encrypted

#gpg-zip -o encrypted-filename.tgz.gpg -c -s file-to-be-encrypted

gpg-zip -o encrypted-filename.tgz.gpg -c -s file-to-be-encrypted

Linux Commands for The BASH SHELL

File Name expansions:

Current user’s home directory: ~/

~/

Current directory: ./

./

Parent directory: ../

Or even (Two parent directories down): ../../

../../

All files in target directory. (Be very careful.): /*

Output Redirects:

Redirect output of one command into the input of another with a pipe: [command 1] | [command 2]

#ps -aux | grep "mysqld"

Or even: [command 1] | [command 2] | [command 3]

Redirect output to a file: [command] > file or [file] > [file]

# ps > file.txt

Or even, to redirect in a different direction: [file] < [file]

#/etc/apt/sources.list < /etc/apt/sources.list

Append output rather than writing over the target file: [file/command] >> [file]

#echo "new line of text" >> /etc/apt/sources.list

Controlling Execution:

Wait until [command 1] is finished to execute [command 2]: [command 1] ; [command 2]

#wget https://wordpress.org/latest.zip; ls;

or evcen: [command 1] ; [command 2]:[command 3] ;

Linux Commands Users and Groups

Change owner of a file or directory: chown

#chown mydir
Change privileges over file or directory: chmod
#chmod 755 /var/www/html

Create a new user: adduser

#adduser john

Change user privileges (be very careful with this one): usermod

#usermod [options] username

Delete user: deluser

#deluser john

Delete user: groups

Create a new group: groupadd

#groupadd groupname

Change group privileges: groupmod

#groupmod [options] groupname

Print usernames of logged in users: users

#users

Linux Commands for Environment and Hardware

Print full date and time: date

#date

Print the hostname of this machine: echo $HOSTNAME

#echo $HOSTNAME

Print information about current linux distro:  lsb_release -a

#lsb_release -a

Print linux kernel version: uname -a

#uname -a

Print information about kernel modules: lsmod

#lsmod

Configure kernel modules (never do this): modprobe

#modprobe

View Installed packages(debain based system): dpkg –get-selections

#dpkg --get-selections

Print environment variables: printenv

#printenv

List hardware connected via PCI ports: lspci

#lspci

List hardware connected via USB ports: lsusb

#lsusb

Print hardware info stored in BIOS: sudo dmidecode

#sudo dmidecode

Dump captured data off of wireless card: dumpcap

#dumpcap

Dump info about keyboard drivers: dumpkeys

#dumpkeys

About the author

Ajay Verma

A Computer Science Graduate, who works extensively on open source projects. His Areas Of interest are: Network Security, Linux Administration, FOSS, Python, and C programming.

Add Comment

Click here to post a comment