[LCLB] L02 - Shell Commands
contents
linux filenames
- linux is flexible about filenames
- file-system doesn’t require extensions to filenames
.sh
: shell script file extension
- filenames are case-sensitive
dot names
- some linux directory names begin with a
.
(dot) - these are typically hidden
- is not listed by the command
ls
- is not listed by the command
ls -a
with the flag-a
has to used to view these dirs- used for caching and configuration
- other things that are not cared about
shell tools
- commands are case-sensitive
- so, even the command-line flags are case-sensitive
command history
- the shell keeps a history of all the commands that were executed successfully
- the
up-arrow
anddown-arrow
keys can be used to scroll through them- hitting enter runs them again
history
command outputs a long list of previously executed commands- including from past shell sessions
Ctrl+R
key-combo at the command-line brings up the ‘reverse-i-search’ function- handy tool to search previously executed commands
- simply start typing in the text string and the command will be populated automatically
common commands
curl -o url/of-file/to-download.ext
- downloads a file from the argument URL to the current dir
unzip things.zip
- takes the filename to unzip as an argument
- the zip files contents get extracted to current dir
- verify with
ls
cat file1.txt file2.txt
- concatenates and outputs contents of two text files
- use
cat file.text
to quickly read the contents of a text file in the prompt
- tab-completion is a shell feature that automatically populates filenames in the command prompt
- conflicts are listed when there are multiple filenames available for tab-completion
- provide additional character and use tab-completion again
ls -l
- along with list of names of elements in current dir
- also shows the modification time
- file size
- dir or not status, and
- permissions for user, groups and everyone
ls -al
is used to see everything in the dir along with all their details
- along with list of names of elements in current dir
file analysis tools
wc file.txt
- outputs the number of lines, number of words and bytes in a text file
diff file1.txt file2.txt
- compares and outputs the differences between two files
- helpful for version comparison and tracking changes between consecutive versions
command manual
man date
- loads manual for the
date
command - all shell commands come with a manual
- synopsis section lists syntax and all the flags/options and arguments for the command
- the description section provides details about all the flags and arguments
- use
q
to exit the manual page
- loads manual for the
researching commands
- use search engines to research commands in addition to the
man
page- sometimes, the explanation is easier to understand on stack-overflow than interpret the manual entry for the command
rm -rf /
is a dangerous command- erases all current user files
- if admin, erases even the root files
rm
: remove/delete things-rf
: recursively forcibly/
: everything in user root dir- doesn’t put it a trash dir
- permanently removes deleted files
different types of command line programs
line based programs
Ctrl+C
: interrupt signal-
Ctrl+D
: EOF (end-of-file) character ping 8.8.8.8
:- check if machine at IP address in argument is alive
- program keep outputting until interrupt signal is sent
sort
: a line based program example- keeps accepting input in new lines
- read the input one line at a time until EOF is entered
- doesn’t process input lines until EOF is sent
- waits for next input line after current input is entered
- when EOF is sent, processed output is displayed in terminal
- the prompt is then ready for the next keyword
programs that wait for input
bc
: simple calculator program- once started, mathematical expressions can be evaluated one after the other
- the program waits for the next expression to evaluate
- after evaluating an expression, it waits for the next expression
- does not return the shell prompt for a new keyword
- to exit the program and get shell back:
- type
quit
- send EOF -
Ctrl-D
Ctrl-C
doesn’t work to return shell prompt
- type
full-screen interactive programs
man
: populates the entire shell screen with the program and hides the shell prompt- to get the shell back, the program has to be exited
- exiting such programs have different keys combos
man
command uses the built-inless
program to display full-screen text
less file.txt
:- opens the text file
file.txt
full screen in the shell - the contents of the text file can be read full-screen
- opens the text file
less
key combos:- use UP/DOWN arrow keys to scroll
D
/Space
: scroll down one pageU
: scroll up one page>
: last line of the file<
: very first line of the file- enter line number at less prompt
:
- then hit ENTER to jump to input line number in text file
/
: activates text search function- enter / and type the search keyword
- use
n
to jump to next match - use
N
to jump to previous match - search is case-sensitive
- reg-exps can be used for search
nano file.txt
: shell text editor- handy to edit config files remotely for example
- opens
file.txt
full-screen shell with text editing enabled - use the key combos listed at the bottom for the operation stated for
^
:Ctrl
key- eg:
^X
(Ctrl-X
) exitsnano
with a save prompt