Upload
andres-gonzalez
View
229
Download
0
Embed Size (px)
Citation preview
8/6/2019 Bash Basics
1/14
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 backspacectrl+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
8/6/2019 Bash Basics
2/14
Command
--help
apropos
cat
cd
cp date
diff
dos2unix
echo
exit/logout
file
grep
head -x
help
history
ignore errrors
info locate
ls -
man
mkdir
more / less
mv
passwd
pwd
rm
rmdir
set
sort tail -x
touch
uname
uniq
unix2dos
uptime
w / who / finger
whereis
which
whoami/who am I
8/6/2019 Bash Basics
3/14
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 -rvdisplay 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
search for pattern in file
display on stdout the first x lines of file (default 10)
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 str
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 co
delete an empty directory
display environment variables
sort then display file on stdoutdisplay 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
display help for bash builtin commands (help alone lists all)
display command history - !number
8/6/2019 Bash Basics
4/14
Command Option
ls
Syntax: ls [options] [file] [directory] ls -l /etc
ls -a "-a"
ls -h "-h"
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"
8/6/2019 Bash Basics
5/14
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.
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.
long listing sorted by name including all files (including hidden files) and shows the file size in human readable format
which will give you a long listing format (which shows more info than just the file names - the owner, size, date last mosorts by name
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.
8/6/2019 Bash Basics
6/14
Command
find / -name filename
find - name "FileName.filenAme"
find -size 100k
find -size +100kfind -size -100k
find -empty -type -f
find -iname "filename"
find -inum [inode number]
find -maxdepth 2 -name filename
find / -maxdepth 3 -name filename
find -mindepth 3 -maxdepth 5 -name f
find -maxdepth 1 -not -iname "pattern
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 -atime
find -mtime
find -ctime
find -mtime +2
find -mtime +2 -mtime -5
find also has the -amin, -cmin, and -m
find -name filename
find -group groupname.
8/6/2019 Bash Basics
7/14
Explanation (default action)
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 100kreturn 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 leve
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 its subdirectory
List all the empty files only in your working directory.
Find only the socket files
Find all directories and subdirectories in the current working directory
Find only the normal files in the current working directory and subdirectories
Find all the hidden files
Find all the hidden directories
find files without ownership
find all files that are owned by root or by www-data
search for all files owned by a user that are not larger than a certain size
search for all files owned by a group
Search by access time
Search by last time file was modified
Search by last time it was changed
look for any files that have not been modified in the last two days
Two or more days ago, but not more than five days ago.
work with minutes instead of days.
find will display the location of the file or files that match that name
find all files that aren'towned by a user
8/6/2019 Bash Basics
8/14
)
l 2 and 3 )
8/6/2019 Bash Basics
9/14
Command
grep [pattern] filenamegrep "literal_string" filename
grep "string" FILE_PATTERN
grep [pattern] -w
grep ["pattern pattern"]
grep [pattern] -i
grep -i "string" FILEgrep -iw "is" demo_file
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_filegrep -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' filesgrep '[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\}'
8/6/2019 Bash Basics
10/14
grep '^.$'
grep '"smug"'
grep '"*smug"*'
grep '^\.'
grep '^\.[a-z][a-z]'
8/6/2019 Bash Basics
11/14
Explanation (default action)
searches filename for pattern anywhere in a lineSearch 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 -iChecking 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 in all files recursively (whole tree)
Searching in all files recursively using grep -r
count maches of whatever pattern
Counting the number of matches using grep -c
find out how many lines matches the patternfind out how many lines that does not match the pattern
Display only the file names which matches the given pattern using grep
Show only the matched string
Show the position of match in the line
Show line number while displaying the output using grep -n
match expression at the start of a line, as in ^A
match expression at the end of a line, as in A$
turn off the special meaning of the next character, as in \^
match any one of the enclosed characters, use Hyphen "-" for a range, as in [0-9]
match any one character except those enclosed in [ ], as in [^0-9]
match a single character of any value, except end of linematch zero or more of the preceding character or expression
match x to y occurrences of the preceding
match exactly x occurrences of the preceding
match x or more occurrences of the preceding
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 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 containing only 'smug'}
8/6/2019 Bash Basics
12/14
{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}
8/6/2019 Bash Basics
13/14
Command
compress zcat
uncompress
gzip gz
gunzip gz
bzip2 bz2
bzcat bz2
bunzip2 bz2
zip zip
unzip zip
tar -f
tar -f tar
tar -jf / tar-f --bzip tbz2
tar -zf / tar-f --gzip tgz
tar -zf / tar-f --compress tZOptions:
-c / -- create
-t / --list
-x / --extract
-v / --verbose
Examples:
tar -cf filename.
tar -cf file.tar file1 file2 file3
tar -czf file.tgz file1 file2 file3
tar -cjf file.tbz2 file1 file2 file3
tar -tjf file.tbz2tar -xzf file.tgz
Note:
file
Extension
8/6/2019 Bash Basics
14/14
Explanation (default action)
compress filesview content of file zipped with compress
decompress files
compress filename into .gz files
decompress .gz files
compress filename into bz2 file
view content of file zipped with compress or gzip
decompress files
compress filename into zip file
decompress files
package together - no compression
compress using bzip2
compress using gzip
compress using compress
create
list
extract
verbose
create an archive containing 3 files
same archive compressed using gzip algorithm
same archive compressed using bzip2 algorithm
list the contents of the bzip2 tar ballextract the contents of the gzip tar ball
will tell compression utility used