Useful Linux Commands

Below is a list of Linux commands covering everything from basic file commands to process management and file permissions.

E&OE*. Use these commands at your own risk**!

If you’ve got any suggestions or feedback for this article, message me here.

Keyboard Shortcuts:

Ctrl + alt + t #Opens a new terminal window.
Ctrl + c #Halts the current command.
Ctrl + d #Log out of session. Same as typing exit
Ctrl + w #Delete one word on the current line.
Ctrl + u #Delete the entire line.
!! #Repeat previous command.


Process Management:

ps #Display active processes.
top #Display all running processes.
kill pid #Kills process id pid
killall proc #Kills all processes named proc


System Information:

man command #Shows the manual page for command.
date #Show current date and time.
uptime #Shows the current system uptime.
whoami #Who are you logged in as.
uname -a #Display kernal information.
lsb_release -a #Display Linux version
df #Show disk usage.
du #Show directory space usage.
free #Shows memory and swap usage.
whereis appname #Lists possible locations for appname
which app #Displays which app will be run by default.
cat /proc/cpuinfo #CPU information.
cat /proc/meminfo #Memory information.


Network Related:

ping host #Pings host until cancelled.
whois domain #Gets the whois record for domain.
dig domain #Returns DNS record for domain.
nslookup domain #Same as dig
tracepath domain/ip #Lists hops to domain/ip
traceroute domain/ip #Same as tracepath
wget file #Downloads file.
wget -c file #Continue a stopped download.
scp file [email protected]:/path/file #Copy local file to [email protected]:/path/file
scp [email protected]:/path/file #Copy file from remote host
scp -r dir [email protected]:/dir #Copy local dir to [email protected]:/dir/
scp -r [email protected]:/dir #Copy dir from remote host


TCPDUMP:

tcpdump -i eth0 #Sniff packets on Ethernet port.

tcpdump switches:
i (Lower Case I) #Interface to sniff.
n #Shows IP and ports instead of hostname.
v #Verbose, could be used up to 3 times.
w file #Sends captured packets to file.

tcpdump expressions:
dst #Traffic destination, could be combined with port or net.
src #Traffic origin, could be combined with port or net.
port (number) #Specific port for packet capture.
portrange #Range of ports to sniff
net #Network to sniff
and #To concatenate (src 10.10.10.10 and dst 10.10.10.11)
or #Record, If either conditions are met (dst port 80 or dst port 443)


FTP:

ftp ftp.server.com #Opens an FTP connection to ftp.server.com
put #Upload a file.
mput #Upload several files (interactive by default).
get #Download a file.
mget #Download several files (interactive by default).
prompt #Disable the interactive option of mput/mget.
cd #Move to a directory.


SSH:

ssh-keygen -t rsa -b 4096 #Generate key pairs for user.
ssh [email protected] #Connect to host as user
ssh -p (port) [email protected] #Connect to host on port as user
ssh-copy-id [email protected] #Add your key to host


Compression:

tar cf file.tar files #Create a tar file named file.tar containing files
tar xf file.tar #Extract the files from file.tar
tar czf file.tar.gz files #Creates a tar file with Gzip compression.
tar xzf file.tar.gz #Extract a tar file using Gzip.
gzip file #Compresses file and renames it file.gz
gzip -d file.gz #Decompresses file.gz back to file

User Management:

adduser user #Simplest way to add a user with prompts.
useradd -d /home/user -m user #Add a user without prompts.
passwd user #Set password for user.

Additional options for useradd:
-a #append
-m #Create home folder
-d #Path to home folder
-s #Shell for the user
-c #Comments for the user
-U #Create group with user
-G #Specify other groups for the user
-e (expiry date) #Date format YYYY-MM-DD
chage -M (days) user #Set password expiration for that user

Learn more about user management in this article.


File and Directory Commands:

ls -al #Formatted directory listing, including hidden files.
pwd #Shows the current directory you're in.
cd - #Switch to previous directory.
cd ~ #Changes to your home directory.
mkdir dir #Creates a directory called dir
rm -r dir #Deletes the directory called dir
rm file #Deletes file
rm -f file #Force removes file
rm -rf dir #Force delete directory dir
cp -r dir1 dir2 #Copy dir1 to dir2 , creates dir2 if necessary.
cp file1 file2 #Copy file1 to file2
mv file1 file2 #Rename file1 to file2. Or move file1 into file2 if directory.
touch file #Creates or updates file
more file #Print file contents to screen.
head file #Print first 10 lines of file to screen.
tail file #Print the last 10 lines of file to screen.
tail +0f file #Display file contents live.


Permissions:

chmod 744 file #Read, write and execute for all.
chmod 755 file #Owner rwx, group and world rx only.
chmod 744 -R /folder #Change all files and folder permissions recursively.
chown -R username:group /folder #Sets owner and group for this folder.

Learn more about Linux file permissions in this article.


Input and Output:

cmd1 | cmd2 #Use the result of cmd1 as input for cmd2 (PIPE).
echo hello > file #Save output to file (Will overwrite if file exists).
echo goodbye >> file #Append output to file
cmd1 < file #Return file contents as input for cmd1
cmd1 & #Execute cmd1 in the background.
cmd1 && cmd2 #Execute if both cmd1 AND cmd2 return true.
cmd1 || cmd2 #Execute cmd2 only if cmd1 returns false (nonzero).


Searching:

grep pattern files #Search files for pattern.
grep -r pattern dir #Recursively search for pattern in dir.
locate file #Search for all instances of file


VIM:

i #Insert mode
esc #Exit back to normal mode
: (Colon) #Command mode

Command mode:
q #Exit (warns if changes made without saving).
q! #Exit without saving changes.
w #Save current changes and continue.
x #Save changes and exit.
/pattern #Search for text pattern
UP ARROW (key) #Shows last command used, same as bash.
split #Divides the screen

Normal mode:
n #Cycles through each instance of search pattern (top to bottom).
d #Delete and copy the line.
x #Delete what is under the cursor.
p (lower case) #Paste after cursor.
P (upper case) #Paste before cursor.
u #Undo last action or command.
ctrl+r #Redo last action or command.
h #Move cursor left.
j #Move cursor down.
k #Move cursor up.
l (Lower case L) #Move cursor right.
0 (Zero) #Jump to first character of the line.
^ (Circumflex) #Jump to first non white character of the line.
$ #Jump to last character of the line.


Installation:

Install from source files:
./configure
make
make install

dpkg -i package.deb #Install from Debian package
rpm -Uvh package.rpm #Install from RPM package

* Errors and Omissions Excepted.

** We are not responsible for any damage or loss of data caused as a result of using any of the commands on this page.