Useful Commands Every Linux User Should Know
The linux command line is a powerful interface that when used correctly can save you a great deal of time. I compiled a list of commands to use as a reference sheet.
These commands are powerful and can be used alone or to make BASH scripts that you can set to run automatically with a cron job.
A good reference on how to make a BASH script with these commands http://linuxcommand.org/writing_shell_scripts.php#contents
System manual
man
Running multiple commands (eg. cd && ls)
&&
Previous Commands
Runs the previous command
!!
View previously executed commands
history
Navigation
Print the working directory
pwd
Used to display contents of current directory.
ls
Useful options for ls:
Displays all files.
ls -a
Shows links, file, owner, and group.
ls -al
Goes to the users home directory or a directory specified
cd
cd /var/www
**Copying**
Used to copy files
cp
Useful options for cp:
cp -f forces it.
cp -r recursive
cp -s makes a symbolic link
cp -u updates file
cp -i prompt before overwriting
cp -p preserves some traits of file (mode,ownership,timestamps)
Moving files and Syncing
Moves file. /example file.txt /example/newlocation
mv
Program to sync locations
rsync
rsync ~/Documents/server /var/www
Useful options for rsync:
rsync -a ~/Documents/server /var/www a preserves symbolic links and other attributes
rsync -v ~/Documents/server /var/www verbose
rsync -r ~/Documents/server /var/www recursive
rsync -z ~/Documents/server /var/www compresses
Finding
Finds files
find
find options:
-name
-iname (ignore case)
-d directory
-f files
-perm 0XXX (replace XXX with permissions
-print prints name
-exec execute a command to files found such as rm
Finds keyword in a file.
grep
Some uses of grep include:
grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2' filename
Find where the files associated with a command are located (replace cd with the command you want)
whereis cd
Reading Files
Display the contents of a file
cat examplefile.txt
Display the last portion of a file (useful for logs)
tail examplefile.txt
**Writing Files**
Update time stamp
touch examplefile.txt
Terminal text editors
nano examplefile.txt
vim examplefile.txt
Echo to location
echo example > example.txt
Managing Files
Change permissions. Replace the XXX with numbers. The first X is user, the second X is group, the third X is world. 1=execute 2=Write 4=Read. Numbers may be combined.
chmod XXX examplefile.txt
Owner of files
chown owner-user file
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file Changes owner and group of a file
Delete files
rm
useful options with rm:
rm -r
rm -f
Compare two files
cmp
Get System Info
uname
Useful options:
-a all
-m machine
-n node
-p processor
-r kernel release
-s kernel name
-v kernel version
-i hardware platform
-o operating system
List devices
lsblk
Get network information
ifconfig
Useful options for ifconfig:
ifconfig eth0 down disable
ifconfig eth0 up enable
ifconfig eth0 192.168.1.XX Replace XX with desired IP
ifconfig eth0 netmask 255.255.255. change netmask
ifconfig eth0 broadcast 192.168.1.255 change broadcast
Shows uptime
uptime
Shows work load
top
Shows network activity
iftop
Shows memory usage
free
Useful options for free:
-b bytes
-k kilobytes
-m megabytes
-g gigabytes
Disk Usage
df
Directory Size
du
Show system processes
ps -a
Kill a process (replace 1234 with the pid number by using ps)
kill 1234
If you know the name of the process you can use the following
pkill name_of_process
Start, restart, and stop services
service service_name start
service service_name restart
service service_name stop
Change Password
passwd
Shortcuts
Shorten the name of commands
alias la='ls -a'
Remove the alias
unalias la
Network
Download a file from the internet
wget www.website.com/directory/filename.fileextention
Ping a website
ping www.example.com
Configure networks
ifconfig
Get MAC address/ip/etc
ifconfig -a
Follow the packet!
traceroute ip_address_goes_here
tracepath ip_address_goes_here
Packet sniffer
tcpdump
See if ports are open
netstat
See what ports are open on a machine
nmap
Enable a connection
ifup eth0
Disable a connection
ifdown eth0
Configure a particular connection
ifcfg
View/Modify routing table
route
Wild Cards
Stand in for any one unknown character
?
Stand in for multiple unknown characters
*
Wild Card & Regular Expression specify a range of characters
[ ]
[a,e,i,o,u]
[a-f]
Specify multiple terms
{*.jpg, picture.*}
Not
[!]
[!important]
Escape character if you want the following symbol to be treated as a normal symbol
/
Regular Expressions
Stand in for any one unknown character
.
Regular Expression Match previous symbol zero or more times (stuf* would match stuff and stufff but not stufg)
*
Combine the previous to to stand in for multiple unknown characters
.*
Specifies first line
^
Specifies end of line
$
OR
|
Not
[^]
Escape character if you want the following symbol to be treated as a normal symbol
/
Uppercase letters
[:upper:]
Lowercase letters
[:lower:]
Alphabetic (letters) meaning upper+lower (both uppercase and lowercase letters)
[:alpha:]
Numbers in decimal, 0 to 9
[:digit:]
Alphanumeric meaning alpha+digits (any uppercase or lowercase letters or any decimal digits)
[:alnum:]
White space meaning spaces, tabs, newlines and similar
[:space:]
Graphically printable characters excluding space
[:graph:]
Printable characters including space
[:print:]
Punctuation characters meaning graphical characters minus alpha and digits
[:punct:]
Control characters meaning non-printable characters
[:cntrl:]
Characters that are hexadecimal digits.
[:xdigit:]
Now to really make use of all of this, you will want to learn how to write a BASH shell script. I will be sure to get a tutorial up soon on how to make your first BASH script.