Learning the Shell Flashcards
date
Outputs date and time per host settings
cal
Outputs calendar for the current month per host settings
df
Outputs the ‘disk free’ / the current amount of free space on your disk drives
ln
[ … how to create a hard link ]
[ … how to create a symbolic link ]
Creates hard and symbolic links
ln FILE LINK => creates a hard link
ln -s ITEM LINK => creates a symbolic link where ITEM is either a file or a directory
rm
Removes files and directories.
Note: use of the ‘-r’ may be required to remove directories on select Linux based OS’s.
mkdir
Creates directories
mv
Moves / Renames flies and directories
mv dir1/fun dir2 => moves the ‘fun’ file from directory 1 (‘dir1) to directory 2 (‘dir2’)
mv fun funner => changes the file / directory named ‘fun’ to ‘funner’
cp
Copies directories or files
cp dir1/fun dir2 => copies the ‘fun’ file in directory 1 (‘dir1’) to directory 2 (‘dir2’)
less
[ … navigation commands ]
View file contents within the command line interface.
Navigation commands:
b / PAGE UP : Scroll back one page
spacebar / PAGE DOWN : Scroll forward one page
up arrow : Scroll up one line
down arrow : Scroll down one line
G : Move to the end of the text file
1G or g : Move to the beginning of the text file
/characters : Search forward to the next occurrence of characters
n : Search for the next occurrence of the previous search
h : Display help screen
q : Quit less
file
Outputs the file type
file FILE
ls
Outputs a list of directory contents
pwd
Print Working Directory : outputs / prints the name of the current working directory
cd
Change Directory : Changes to the specified directory
On the desktop there is a folder named ‘dir1’
From Desktop : cd dir1 => moves the operator to the working directory to ‘dir1’
exit
bye
Exit or Bye can be used to end the terminal session
free
Outputs amount of free memory on the host
type
Outputs how a command name is interpreted
type ls => ‘ls is /bin/ls’
[ ^ from MacOS ]
which
Outputs which executable program will be executed
which ls => /bin/ls
[ ^ from MacOS ]
man
Outputs a command’s manual page – i.e. the detailed documentation of the command and its options
man COMMAND
apropos
Outputs a list of appropriate commands like a search function
apropos COMMAND
info
Outputs a command’s info entry
info COMMAND
whatis
Outputs a brief description of a command
whatis COMMAND
alias
Creates an alias for a command durable for the terminal session.
if ‘cd /user; ls; cd -‘ is a common line, then
alias foo=’cd /user; ls; cd -‘, then typing
‘foo’ will execute ‘cd /user; ls; cd-‘ for the remainder of the terminal session.
cat
Concatenates files
you have movie.mpeg.001 … movie.mpeg.099 =>
cat movie.mpeg.0* > movie.mpeg
^ concatenates the files into a single file
sort
Sort lines of text
uniq
Reports on or omits repeated lines
wc
Print newline, word, and byte counts for each file
grep
Outputs lines matching a pattern
grep STRING, for example ‘ls /bin | grep zip’ will return all items listed in ‘/bin’ that contain ‘zip’
head
Outputs the first part of a file
tail
Outputs the last part of a file
tee
Read from standard input and write to standard output and files
echo
Outputs a line of text
echo ‘Hello World!’ => ‘Hello World!’
clear
Clears the screen
history
Outputs the contents of the history list
ps
Outputs a snapshot of current processes
top
Outputs tasks
jobs
Outputs a list of active jobs
Long listing fields
-rw-r–r– 1 root root 32059 2012-04-03 11:05 oo-cd-cover.odf
- rw-r-r– : Access rights to the file.
- : The first character indicates the type of file. Among the different types, a leading dash (‘-‘) means a regular file, while a ‘d’ indicates a directory.
‘rw-‘ : The next three characters are the access rights for the file’s owner.
‘r–’ : The next three are for members of the file group.
‘r–’ : The final three are for everyone else.
1 : File’s number of hard links.
root : The user name of the file’s owner.
root : The name of the group that owns the file.
32059 : Size of the file in bytes.
2012-04-03 11:05 : Date and time when the file was last modified.
oo-cd-cover.odf : Name of the file.
Redirecting standard output
There are two options:
1: ‘>’ overwrites
e.g. ls -l /usr/bin > ls-output.txt => creates .txt
ls -l /usr/bin > ls-output.txt => overwrites the above
2: ‘»’ appends
e.g. ls -l /usr/bin»_space; ls-output.txt => creates .txt
ls -l /usr/bin»_space; ls-output.txt => appends a second output of /usr/bin to the .txt created, above
Note:
0 = standard input
1 = standard output
2 = standard error
Redirecting standard error
‘2>’
ls -l /usr/bin 2> ls-error.txt => ‘2>’ redirects standard errors to ‘ls-error.txt’
Note:
0 = standard input
1 = standard output
2 = standard error
Redirecting standard output and standard error
There are two options:
- Original version:
ls -l /usr/bin > ls-output.txt 2>&1^ redirects standard output, then makes standard error ‘2’ and input to standard output ‘>&1’ - New jack version:
ls -l /usr/bin &> ls-output.txt^ redirects standard input and error ‘&>’ to ‘ls-output.txt
Pipelines
COMMAND1 | COMMAND2
COMMAND1 becomes input for COMMAND2
Pipelines are used to ‘filter’ as in:
ls /bin /usr/bin | sort | less
^ list contents of ‘/bin’ and ‘/usr/bin’, then input to ‘sort’ to order the list, the ‘less’ to output to terminal
- or -
ls /bin /usr/bin | sort | uniq | less
^ as previous, but, sort, then deduplicate (‘uniq’), then ‘less’ to output to terminal
Expansion
1) pathname expansion
2) tilde expansion
3) arithmetic expansion
4) brace expansion
5) parameter expansion
Expansion is the process by which elements of command are expanded by the interpreter.
1) pathname expansion: expansion of the wildcard (‘ * ‘)
e. g. echo D* => all items starting with ‘D’
2) tilde expansion: expansion of the ‘ ~ ‘
e. g. echo ~ => /home/me (the current user)
3) ' $((2+2))' => 4 ' + ' addition ' - ' subtraction ' * ' multiplication ' ** ' exponentiation ' / ' division (no remainder) ' % ' modulo / reminder (only the remainder)
4) brace expansion: use of ‘ { } ‘ to create multiple text strings from a pattern contained within the braces.
e. g. ‘ file-0{1..5} ‘ => creates five directories named file-01, file-02, file-03, file-04, and file-05
5) parameter expansion : taps the system’s ability to store small chunks of data and to give each chuck a name.
e. g. echo $USER => me (the current user)
id
Display the user identity.
chmod
Change a file’s mode.
umask
Set the default file permissions.
su
Run a shell as another user.
generic form: su [-[l]] [user]
If the ‘ -l ‘ option is included, the resulting shell session is a login shell for the specified user. This means that the user’s environment is loaded and the working directory is changed to the user’s home directory.
sudo
Execute a command as another user.
chown
Change a file’s owner.
chgrp
Change a file’s group ownership.
pssswd
Change a user’s password.
generica form: passwd [user]
File Types
- d l c b
’ - ‘ A regular file.
’ d ‘ A directory.
’ l ‘ A symbolic link. Notice that with symbolic links, the remaining file attributes are always rwxrwxrwx and are dummy values. The real file attributes are those of the file the symbolic link points to.
’ c ‘ A character special file. The file type refers to a device that handles data as a stream of bytes, such as a terminal or modem.
’ b ‘ A block special file. This file type refers to a device that handles data in blocks, such as a hard drive of CD-ROM drive.
File Modes in Octal
0 1 2 3 4 5 6 7
Octal Binary File Mode 0 000 - - - 1 001 - - x 2 010 - w - 3 011 - w x 4 100 r - - 5 101 r - x 6 110 r w - 7 111 r w x
^ in File Mode column, spaces added for readability: e.g. rwx becomes r w x
CHMOD Symbolic Notation
u
g
o
a
’ u ‘ Short for ‘user’ but means the file or directory owner.
’ g ‘ Group owner.
’ o ‘ Short for ‘others’ but means world.
’ a ‘ Short for ‘all;’ the combination of u, g, and o.
^ if no character is specified, ‘all’ will be assumed.
CHMOD Symbolic Notation Examples
u+x u-x \+x o-rw go=rw u+x,go+rx
’ u+x ‘ Add execute permission for the owner.
’ u-x ‘ Remove execute permission from the owner.
’ +x ‘ Add execute permission for the owner, group, and world. (Equivalent to a+x.)
’ o-rw ‘ Remove the read and write permissions from anyone besides the owner and group owner.
’ go=rw ‘ Set the group owner and anyone besides the owner to have read and write permission. If either the group owner or world previously had execute permissions, remove them.
’ u+x,go+rx ‘ Add execute permission for the owner and set the permissions for the group and others to read and execute. Multiple specifications may be separated by commas.
ps
Report a snapshot of current processes.
top
Display tasks.
jobs
List active jobs.
bg
Place a job in the background.
fg
Place a job in the foreground.
kill
Send a signal to process.
killall
Kill processes by name.
shutdown
Shut down or reboot the system.