Unix Basics
This article is part of my personal wiki where I write personal notes while I am learning new technologies. You are welcome to use it for your own learning!
Basic Commands
Files and Directories
- List files
ls- List files with inode numbers
ls -i - List all
ls -a - List in long format (more details0
ls -l - Mark directories with a trailing slash and executable files with a trailing asterisk
ls -F - Recursively lists subdirectories encountered
ls -R - Gives the size of each file
ls -s - Sorts by time modified instead of by name
ls -t
- List files with inode numbers
- Copy file
cp(cp file1.txt file2.txt) creates a duplicate- Copy directories
cp -ras in recursively (cp -r cookies cookies2) - The interactive flag prompts you whether you want to overwrite a file or not
cp -i
- Copy directories
- Move file
mv(mv file1.txt file.txt)- Also interactive with
mv -i - Don’t use wildcards
*with mv unless you move to a directory. Themvcommand doesn’t know what to do if you specify a target file name, it will just put them all on top of each other
- Also interactive with
- Remove file
rm(rm file1.txt)- Remove directories
rm -r
- Remove directories
- Print text in command line
echo(echo hello wold) - Redirect output to file
>(echo hello world > file.txt) - Create link
ln(ln file.txt link.txt). Links to the same index node (inode) that other file. - Create symbolic (soft) link
ln -s(ln -s file.txt slink.txt). Links to a filename. Useful when linking files between different filesystems.- You can only create symbolic links to folders
ln -s(ln -s cookies cookies3)
- You can only create symbolic links to folders
- Manual
man(man ls) alsoman [section] name(man 5 ls)- If you don’t know the name of the command exactly you can use
apropos(apropos copywill search all headlines for the word copy).man -kdoes the same asapropos
- If you don’t know the name of the command exactly you can use
- Catenate
cat- Print file contents
cat file.txt - Print multiple file contents
cat file.txt file2.txt - Write files to another file (catenating)
cat file.txt file2.txt > files.txt
- Print file contents
- Display in a paged manner with
more- See contents of a file
more filename- scroll entire screen with
spacebar, a line withENTER - enter
viby typingv - quit reading with
q
- scroll entire screen with
- Display the output of other program in a paged manner with
|likecommand | more
- See contents of a file
- Print working directory
pwd - Create directory
mkdir(mkdir cookies)- Remove directory
rmdir(rmdir cookies)
- Remove directory
- Change directory
cd(cd cookies). Current directory.. Parent directory...
Paths
- Relative paths
lala/lala/lala(start in the directory you are in) - Absolute paths
/lala/lala/lala(start in the root directory) - Wildcards
*- list all txt files
ls *.txt - list all txt files that start with cheese
ls cheese*.txt - list all txt files in all subdirectories of current dir
ls */*.txt
- list all txt files
- If you have spaces in your path,
- use quotation marks
cd "john doe" - escape it with a backslash
ls john\ doe
- use quotation marks
Wildcars in the Shell
*match any number of characters in a filename or directory, including none?match any single character[]enclose a series of characters that can be matched at a specific position-within[]denotes a range of characters~at the beginning of a word expands to the name of your home directory. If you append a username to it, then it’s that user’s home directory.- Redirecting input/output
<send input to a command>create a file and send output to it>>append output to an existing file
- Pipelining
- pipes are a convenient way to channel the output of a command to the input of another command without creating an intermediate file
ps -aux | sort | morewould give you a paginated alphabetical list of all running processes- note that if a command cannot read from standard input then it cannot be put to the left of a pipe
- Repeat commands from history
!!repeat last command!gitrepeat most recent command that starts withgit?statusrepeat most recent comand that containsstatus
Deleting Stuff
- Delete world
CTRL+w - Delete line
CTRL+u
Control Terminal Output and Programs
- Suspend output
CTRL+s - Abort program
CTRL+c - Discard output
CTRL+ountil anotherCTRL+ois typed - Susped program
CTRL+z - See all current running programs
jobs - Resume suspended program in the foreground with
fgand in the background withbg
User Session
- Log in remotely
rlogin - Change passwrod with
passwd - Exit
exitorCTRL+d(useset ignoreofto avoid exiting by mistake when you typeCTRL+d)
Permissions
- find out which user you are logged in as
whoami - find out which gorup you belong to
groups - execute a file
- unless a file is part of your path you need to prepend the path to it to run it
./file.sh
- unless a file is part of your path you need to prepend the path to it to run it
- Display permissions through
ls -l - Change permissions using
chmod- change user permisions
chmod u+rwx file.txt(add rwx to user) - change group permissions
chmod g+rwx file.txt(add rwx to group) - change others permissions
chmod o+rwx file.txt(add rwx to others) - you can combine them at once
chmod ugo+rwx file.txt - use a
-sign to remove permissions - you can also use numbers from
0(no permissions) to7(all permissions) f.i.chmod 777(all permissions for user, group and others).755and754are common combinations (restrict write to groups and others, restrict write to groups and write,execute to others)
- change user permisions
More Useful Commands
aliaslets you create shorcuts. For instance,alias rm 'rm -i'. UNIX substitutes command 1 with command 2 when you type it in the terminal- You can remove an alias with
unalias
- You can remove an alias with
apmagic autopilot commandcompressanduncompresscompresses using adaptive Lempel-Ziv algorithm (packunpackalso compresses using Huffman)dfshows the space free on diskdifflets you diff two files.diff -bignores white spacediff -iremoves case sensitivity
duprovides disk usage informationechoechoes a string into the terminal. Useful to see the content of environment variables (echo $TERMorecho $PATH)findfind files recursively in a directory matching a logical expressionfingershows information about users. It can be used locally and across the internetsftpto copy files across the networkgrep(get regular expression) searches for a expression in a file or group of files.grepexpands wildcard characters in the given expressionegrep(extended grep) searches the expression including alternations (usually faster than grep/fgrep)fgrep(fixed-string grep) only searches for the string and doesn’t expand wildcards- Wildcard characters
\\: escape wildcard character after\\[]: match any character within brackets (grep [cm]an *.js).: match any character (grep eg. *.js)^: match beginning of line$: match end of line*: match 0 or more of the preceding character
- Useful options
-fmatches all the expressions in a given file as opposed to the one typed in the command line-iremoves case sensitivity-ndisplays the line numbers containing a match-ldisplays the names of the files that contain a match but not the lines that contained a match-vdisplays lines that don’t match
- Examples
- Find all javascript files with variable
foo:egrep foo *.js(add the option-nto get line numbers)
- Find all javascript files with variable
historydisplays a list of the commands you’ve previously typed (you must have configured it viaset history=nwherenis the number of commands to save)killkills a program (f.i.kill -9 processidusepsto retrieve theprocessidof a program)- if the process was started by the current cli you can use
jobsto get the process index andkill -9 %n
- if the process was started by the current cli you can use
lookuses the system dictionary to search for a word. Useful for correcting spelling- you can also use
spellto check spelling
- you can also use
ps(process status) shows the status of currently running processesscriptrecords everything you type in a terminal and every that is outputed within a file (script logsession.txt)setenvlets you set environment variables (setenv CUCUMBER 200)sourcesends the contents of a file into the UNIX shell.- If you have a series of aliases that you usually use, you can use
source .myaliasesinstead of typing them yourself
- If you have a series of aliases that you usually use, you can use
tar(tape archiver) lets you store a bunch of files in a single file. (tar key name)- c Creates a new tape
- f Used for taring to a tape
- t Lists the contents of a tar file
- v Turns verification on
- x Extracts selected files. If no file argument is given the entire contents of the tar file is extracted
- For instance
- create tar file
tar cfv mysourcedir/files directory - extract tar file
tar xfv mysourcedir/files directory
- create tar file
- For more stuff go to UNIX is a 4 letter word
References
- The Linux Command line
- UNIX is a 4 letter word
- Code School Unix Basics Part I and Part II

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.
