session ii basic commands - freedomyug.files.wordpress.com · session ii basic commands by gourav...
TRANSCRIPT
Linux Training
Session II
Basic CommandsBy
Gourav Shahhttp://freedomyug.wordpress.com/
Topics
Command Line Basics
Working with directories
File Manipulations
Command Line Basics
Shell Variables
Executing Commands
Command sequences
Command history
Applying commands recursively
Shell Variables
Shell Variable - a named storage location that contains a value
Bash maintains a set of shell variables which contain imp info for execution of bash
e.g. $PS1 , $PATH
Environment Variables – shell variables used during the execution of the programs.
Setting up environment variable
e.g export MYVAR
Executing Commands
Anatomy of a command,
cp -r abc xyz
“cp” : a valid command “-r” : command option / switch “abc , xyz” : arguments
e.g. ls -al
Command Sequences
How to enter multiple commands on a single line ?
Ans. use command separator ( ; ) after each command except the last one
e.g. ls ; ps
Command History History is useful to repeat/modify long and
complex command sequences executed in the recent past.
Command history expansion designators,!! : most recent command!n : refer to command 'n' from history. Use
command history to find out n!-n : current command -n!string: most recent command starting with
string!? : most recent command containing string
Applying Commands Recursively
Using wildcards e.g. chmod +x *.sh
Recursive option -r -R --recursive e.g. chmod -R tmp
Inherently recursive commands e.g. Find /home/ses -name “*.txt”
Working with directories
Listing contents of the working directory
Creating directories
Moving in directories
Removing directories
Listing Contents of a Directory
Command : ls Description : list directory contents
Options -a, --all : list entries including hidden files (.xx)-l : use long listing format
e.g. ls ls -l ls -l -all ls -al
Creating Directories
Command: mkdirDescription: create one or more directoriesoptions:-m : set access mode for dirs-p : create parent dir if it doesn't exist
e.g mkdir amar mkdir akbar anthony mkdir -p amar/akbar/anthony mkdir -m 777 movie
Moving in Directories
Command : cdDescription: change working directory
Command : pwdPurpose: print working directorye.g. cd abc cd /home/ses/abc ( absolute path) pwd cd .. ( move to parent dir)
Removing Directories
Command : rmdirDescription: removes empty directoriesOption: -p : removes dirs and parent dirs
e.g.
rmdir -p amar/akbar/anthonyrmdir akbar
File Manipulations
Display file – more, less, cat
Copy file – cp
Rename/Move file – mv
Remove files - rm
Find file – find
Copying files between machines – scp, rsync
Displaying Files
Commands : less, moreDescription: output text one screen at a time
e.g more blah-blah.txt less blah-blah.txt
Command : catDescription: concatenate files and print on the
standard output
e.g cat blah-blah.txt
Copying Files
Command: cpDescription: copy files and directoriesOptions: -f : force an overwrite-r, -R : recursive copy-v : verbose Syntax : cp [options] source dest
e.g. mkdir home office touch home/room.txt cp home/room.txt office/
Renaming/Moving Files
Command: mvDescription: rename/move files Syntax: mv [options] src dest
e.g. Renaming a file
touch rajnimv rajni the_boss
Moving a filemv the_boss movies/
Removing Files
Command : rmDescription : remove files or directoriesOptions:-f , --force : force remove-r , -R : recursive (remove dir contents)-v : verbosee.g.
rm foo.txtrm -rf /home/ses/foo/*
***always be careful when using rm command
Finding Files
Command : findDescription: search for files in a directory
hierarchySyntax : find [path] [expression]
e.g.
find /home/ses/ -name “*.py”
Remote File Manipulation
Command : scpDescription: secure copy (remote copy program)Options: -r : recursiveSyntax: scp [options] src dest
If src/dest is remote then use 'user@host:path'
e.gscp foo ses@zene:/home/ses/scp release@brownback:~/foobar.csv /home/ses/
Remote File Manipulation
Command: rsyncDescription: copies files to or from a remote
host. Transfers only differences between two sets of files.
Options: -a : archive, preserver everything-v : verbose-z : compression
Syntax: rsync [options] src dest'user@host:path' for the remote machinee.g. rsync -avz /src/bar ses@zene:/tmp rsync -avz /src/bar/ ses@zene:/tmp