Bash Basics
Bash Basics
Keyboard Use
ctrl+a move cursor to beginning of line
ctrl+c end running program
ctrl+d exit or logout
ctrl+e move cursor to end of line
ctrl+h backspace
ctrl+l clear screen
ctrl+r search back through command
ctrl+z suspend a program (not ending a program as ctrl-c)
left/right arrows move the cursor
up/down arrows browse through command history
shift+page up browse forward through terminal buffer
shift+page down browse back through terminal buffer
tab command/file name completion
tab+tab command/file name completion possibilities
BASH Basics
Command
<command> --help
apropos <pattern>
cat <filename>
cd <directory>
cp <fn1> <fn2>
date
diff <fn> <fn>
dos2unix
echo
exit/logout
file <filename>
grep <pattern> <fn>
head -x <fn>
help <command>
history
ignore errrors
info <command>
locate <cmd>
ls -<options> <dir>
man <command>
mkdir <fn>
more / less
mv <fn1> <fn2>
passwd
pwd
rm <filename>
rmdir <fn>
set
sort <fn>
tail -x <fn>
touch
uname
uniq <fn>
unix2dos
uptime
w / who / finger
whereis <cmd>
which <cmd>
whoami/who am I
BASH Basics
Explanation
display list of options for the command
search a database of command descriptions for pattern / to use when can't remember a command
display the contents of a file
change current working directory / "cd -" toggles between two directories
copy file and directories - cp ~/* ./copies -rv
display systems date and time on stdout
compare fn1 to fn2 display on stdout the lines that differ
convert text file to Linux format
display text on stdout
end the bash session
display the type of the file <filename>
search for pattern in file
display on stdout the first x lines of file (default 10)
display help for bash builtin commands (help alone lists all)
display command history - !number
2>/dev/null
help files (info info for help on info)
search a database of file system information for cmd
display a listing of the directory or file some options: r=reverse a=all R=recurse l=long
help files (man man for help on man) / q = quit / space bar = forward / b = backwards / "/ string" = search / n = next string / N =
create a new directory
display on stdout one screen at a time / space bar = page forward / b = page backwards / q = quit / "/string" = search
rename / move fn1 to fn2 or rename and move at the same time fn1 fn2
change password of the current user
display current working directory (print working directory)
delete a file or directory - Ex: "rm * -r /" Ex2: rm * -rf = delete recursively and forces the deletion - so it won't ask for confirmatio
delete an empty directory
display environment variables
sort then display file on stdout
display on stdout the last x lines of file (default 10)
update the time stamp / create a file if it doesn't exist / syntax = touch filename
display system information
remove adjacent duplicate lines from file then display on stdout
convert text file to Windows format
display how long system has been running
show logged on users
search standard Linux directories for cmd
search the directories in the environment variable PATH for cmd
display on stdout current effective userid
Command Option
ls
ls -l "-l"
ls -r "-r"
ls -R "-R"
ls -s "-s"
ls -S "-S"
ls -t "-t"
Examples
ls -l /etc
ls -l /home/rich/www
ls -al /home
ls -ash
ls -lash "-lash"
Description
List directory contents
The ls command will list the files and directories within the current working directory (the directory you are currently in).
A file or directory can be specified - wildcards like * can be use to refer to several files.
will show you ALL the files in the directory, including hidden files
will show the size in "human readable format" (ie: 4K, 16M, 1G etc). Must be use in conjunction with the -s option.
which will give you a long listing format (which shows more info than just the file names - the owner, size, date last modified et
sorts by name
reverse order while sorting
will the subdirectories recursively, which means it will show ALL the directories and files within the specified directory.
will also show you the size of the files (in blocks, not bytes)
sorts by file size
sorts by modification time
Lists ALL the files and directories in the /home directory, in the long listing format.
Lists ALL the files in the current directory (no directory was specified so it lists the contents of the current directory),
and the size of the files/directories, written in 'human readable' format.
long listing sorted by name including all files (including hidden files) and shows the file size in human readable format
Command
find -name filename
find / -name filename
find - name "FileName.filenAme"
find -size 100k
find -size +100k
find -size -100k
find -empty -type -f
find -iname "filename"
find -inum [inode number]
find ~ -empty
find . -maxdepth 1 -empty
find -type s
find -type d
find -type f
find . -type f -name ".*"
find -type d -name ".*"
find -nouser
find -user root -o -user www-data
find -not -user www-data
find -user www-data -not -size +100k
find -group groupname.
find -atime
find -mtime
find -ctime
find -mtime +2
find -mtime +2 -mtime -5
find also has the -amin, -cmin, and -mmin
Explanation (default action)
find will display the location of the file or files that match that name
find file under all sub-directories starting from root directory.
use " " to search by filename exactly as specified
find files that are 100k in size
return any files larger than 100k
return anything smaller.
find files that are empty (-type -f so find does not include directories as empty files)
Find Files Using Name and Ignoring Case
Finding Files by its inode Number
Find file under root and one level down. (i.e root — level 1, and one sub-directory — level 2)
Find file under root and two levels down. (i.e root — level 1, and two sub-directories — level 2 and 3 )
Find file between sub-directory level 2 and 4
Shows the files or directories whose name are not pattern only under current directory
Find all empty files (zero byte file) in your home directory and it’s subdirectory
List all the empty files only in your working directory.
grep [pattern] -v
grep -v "go" demo_text
grep -v -e "pattern" -e "pattern"
grep [pattern] -r
grep -r "ramesh" *
grep [pattern] -c
grep -c "pattern" filename
grep -c this demo_file
grep -v -c this demo_file
grep -l this demo_*
grep -o "is.*line" demo_file
grep -o -b "pattern" file
grep -n "go" demo_text
^ (Caret)
$ (Question)
\ (Back Slash)
[ ] (Brackets)
[^ ]
. (Period)
* (Asterisk)
\{x,y\}
\{x\}
\{x,\}
Examples:
grep -I -w [pattern] *
grep smug files
grep '^smug' files
grep 'smug$' files
grep '^smug$' files
grep '\^s' files
grep '[Ss]mug' files
grep 'B[oO][bB]' files
grep '^$' files
grep '[0-9][0-9]' file
grep b.b
grep '^From: ' /usr/mail/$USER
grep '[a-zA-Z]'
grep '[^a-zA-Z0-9]
grep '[0-9]\{3\}-[0-9]\{4\}'
grep '^.$'
grep '"smug"'
grep '"*smug"*'
grep '^\.'
grep '^\.[a-z][a-z]'
Explanation (default action)
searches filename for pattern anywhere in a line
Search for the given string in a single file
Checking for the given string in multiple files
searches for a whole word
use "pattern" to search for strings including spaces and special characters
ignore case while searching - grep is case sensitive
Case insensitive search using grep -i
Checking for full words, not for sub-strings using grep -w
display the lines that do not match the specified pattern / searches for everything except the keyword provided
Invert match using grep -v
display the lines which does not matches all the given pattern.
searches files in the current working directory for pattern in any case
{search files for lines with 'smug'}
{'smug' at the start of a line}
{'smug' at the end of a line}
{lines containing only 'smug'}
{lines starting with '^s', "\" escapes the ^}
{search for 'Smug' or 'smug'}
{search for BOB, Bob, BOb or BoB }
{search for blank lines}
{search for pairs of numeric digits}
search for "bob", "bib", "b-b"
{list your mail}
{any line with at least one letter}
{anything not a letter or number}
{999-9999, like phone numbers}
{lines with exactly one character}
{'smug' within double quotes}
{'smug', with or without quotes}
{any line that starts with a Period "."}
{line start with "." and 2 lc letters}
Command Extension
compress <filename>
zcat
uncompress <filename>
gzip <filename> gz
gunzip <filename> gz
Examples:
tar -c<insert options>f filename.<insert extention>
tar -cf file.tar file1 file2 file3
tar -czf file.tgz file1 file2 file3
tar -cjf file.tbz2 file1 file2 file3
tar -tjf file.tbz2
tar -xzf file.tgz
Note:
file <filename>
Explanation (default action)
compress files
view content of file zipped with compress
decompress files
create
list
extract
verbose