Common Unix Commands

UNIX BASICS

$ ls -la
list all files including hidden files in current directory

$ ls *.txt
list all files that end with .txt in current directory

$ ls -R *.txt
list all files that ned with .txt in current directory and all children directories

$ pwd
print working directory

$ cd images
change directory to images directory

$ cd ~
change to user home directory

$ cd ..
change to one directory up

$ touch foo.txt
creates foo.txt or changes its last modified time to the current time

$ mkdir foo
make directory foo

$ cp foo.txt foo_bak.txt
make a copy of file foo.txt and name it foo_bak.txt

$ cp foo foo2
make a copy of directory foo and name it foo2

$ mv foo.txt bar.txt
renames (moves) foo.txt to bar.txt

$ mv foo.txt ~/bar.txt
Wrenames foo.txt to bar.txt and moves it to home folder

$ rm -rf projects
deletes all files and folders under the project folder recursively

$ su jdoe
substitute user to user jdoe

$ su
substitute user to become the root user

$ whoami
displays who the current user is

LEARNING ABOUT COMMANDS

$ man ls
get information (manual) for the ls command

$ whereis curl
find the path to where curl is installed / located

BUILDING BLOCKS

$ ls -l | less
pipe the output of one command as input into another

$ ls -l > foo.txt
redirect output of a command to a file

$ ls -l >> foo.txt
append a command’s output to a file

$ echo < numbers.txt
1/
2/
3/
use a file as input for a command

VIEWING FILES

$ cat foo.txt
view file on screen (stdout)

$ less foo.txt
view file one screen at a time

$ head foo.txt
view first 10 files of a file

$ tail foo.txt
view last 10 lines of a file

$ tail -f foo.txt
view the constantly-updated last lines of a file

OWNERSHIP & PERMISSIONS

$ chgrp admins foo.txt
change the group owning the file foo.txt to “admins”

$ chgrp -R images
recursive change the group owning the the directory “images”

$ chown jdoe foo.txt
change the owner of file foo.txt to jdoe

understand the basics of permissions

$ chmod u=rwx foo.txt
$ chmod g+w foo.txt
$ chmod a-rw
syntax: chmod [ugo][+-=][rwx]
change the permissions of foo.txt for user, group, all

$ chmod -R g+w images
change permissions of images folder and all below it recursively to be group writable

ARCHIVE & COMPRESS FILES

$ zip foo.txt bar.txt
zip the two files into one

$ unzip foo.zip
unzip the archive foo.zip

$ gzip foo.txt
compress file foo.txt

$ gunzip foo.txt.gz
unzip foo.txt.gz

$ tar -cf images.tar *.jpg
archive files with tar

FINDING STUFF

$ locate images
list all files with name “images” in them

$ grep honda list-of-cars.txt
search inside list-of-cars.txt for patterns matching “honda”

$ grep honda *
search inside all files in current directory for patterns matching honda

$ grep “hey you!” *
search for “hey you!” without quotes in all files in current directory

$ grep -R “hey you!” *
same as above except recursively

$ ls -l | grep honda
search the output of a command for specific words

$ ls -l | grep -v honda
show lines where words do not appear in files (-v is to invert matches)

$ grep -l sprite_*.jpg
list files containing search for words

THE FIND COMMAND

$ find . -name “sprite_*”
file files by name in current directory that match sprite_*

YOUR SHELL

$ history
view your command line history

$ alias
display all command aliases

$ alias l=’ls -la’
create a new temporary alias

$ unalias l
remove an alias

MONITORING SYSTEM RESOURCES

$ ps aux
view all currently-running processes

$ ps U jdoe
view processes owned by a particular user

$ kil 27921
end running process with process id (pid) 27921

$ free
display information about system RAM

$ df
show file system disk usage

$ du -hs
report file space (disk usage) used by a directory

CONNECTIVITY

$ ping www.google.com
verify that a computer is running and accepting requests

$ host www.qualys.com
perform dns lookup

$ host 65.214.39.152
perform reverse dns lookup

WORKING ON THE NETWORK

$ ssh [email protected]
securely log in to another computer

scp, sftp, rsync, wget