Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Linux - Part 1 -

The document provides a comprehensive overview of essential Linux commands, including filesystem navigation, file manipulation, and text handling. It covers command usage, options, and examples for commands like cp, mv, ls, grep, and find, as well as concepts such as absolute and relative pathnames, redirection, and piping. Additionally, it explains wildcard characters and globbing, along with standard input/output operations in Linux.

Uploaded by

mani.hen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Linux - Part 1 -

The document provides a comprehensive overview of essential Linux commands, including filesystem navigation, file manipulation, and text handling. It covers command usage, options, and examples for commands like cp, mv, ls, grep, and find, as well as concepts such as absolute and relative pathnames, redirection, and piping. Additionally, it explains wildcard characters and globbing, along with standard input/output operations in Linux.

Uploaded by

mani.hen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Linux Commands

Linux command review


• Help
• Filesystem
• Text Handling
• Redirection
• Piping
Help commands
• whereis
• whatis
• man
• apropos
• info
• --help
File and Directory Names
• Names may be up to 255 characters
• All characters are valid, except the forward- slash
• It may be unwise to use certain special characters in file or
directory names
• Some characters should be protected with quotes when
referencing them
• Names are case-sensitive
• e.g, MAIL, Mail, mail, and mAiL are different files
• Again, possible, but may not be wise
Absolute and Relative Pathnames
• Absolute pathnames
• ALWAYS begin with a forward slash
• Complete "road map" to file location
• Can be used anytime you wish to specify a file name
• Relative pathnames
• NEVER begin with a slash
• Specify location relative to your current working directory
• Can be used as a shorter way to specify a file name
Filesystem commands
• pwd – display current directory
• as absolute pathname
• cd – change directory
• ls – list directory contents
Filesystem Search commands
• find – find files in a directory tree
• which – find command in a directory PATH
• PATH=/usr/bin;/bin/;/usr/sbin;/usr/local/bin
• whereis – show pathname to command
• locate – find pathnames that match
pattern
File and Directory Commands
• cp – copy a file or directory
• mv – move or rename a file or directory
• touch – create a file
• rm – remove a file or directory
• mkdir – create a directory
• rmdir – remove an empty directory
• file – determine file type

Navigating the Filesystem (38:13)


Linux directory structure
Relative pathname exercises
From /home/his/bin, cd to
• /home/his/notes
• /home/his
• /home/sam
• /etc
•/
using relative pathnames; e.g. cd ..
Change Directory
• cd - change directory
• cd /home/joshua/work // absolute path
• cd project/docs // relative path
• To a directory one level up: cd .. // <dot><dot>
• To your home directory: cd // no argument
• To your home directory: cd ~ // <tilde>
• To your previous working directory: cd - // <dash>

5 Basic Linux Commands (7:14)


List Directory Contents
ls – list directory contents
• Usage: ls [options] [<pathname>]
• ls -a (include hidden files)
• ls -l (long display of extra information)
• ls -R (recurse through directories)
• ls -ld (directory and symlink information)
• ls -1 (shows information in a single column)
• ls –t (shows most recently modified files first)

NOTE: file beginning with a dot are hidden (.bashrc)


ls –l output
Filetypes
ls -l symbol File Type
- regular file
d directory
l symbolic link
b block special file
c character special file
p named pipe
s socket
Copy Files and Directories
• cp - copy files and directories
• Usage:
• cp [options] file destination
• More than one file may be copied at a time if the
destination is a directory:
• cp [options] file1 file2 dest

$ cd // change to home directory


$ cp /etc/passwd . // copy file to current
directory
$ cp /etc/groups /etc/hosts /home/labuser
// copy two files to /home/labuser
cp File Scenarios
• cp file1 to existing dir1
• Creates file1 in dir1
• cp file1 to non-existing dir1/dir2
• Creates file dir2 in directory dir1
• cp file1 to existing file2
• Overwrites file2
• cp file1 to non-existing file2
• Creates file2
Use –p option to preserve file attributes
cp Directory Scenarios
• cp dir1 to existing dir2
• Creates directory dir2/dir1
• cp dir1 to non-existing dir2/dir3
• Creates directory dir3 in directory dir2
• cp dir1 to existing file1 (invalid)
• cp dir1 to non-existing file1
• Creates directory with new name file1
Must use –r option; entire directory tree is copied
Use –p option to preserve directory attributes
Move/Rename Files and Directories
• mv – move/rename files and directories
• Usage:
• mv [options] file destination
• mv [options] directory destination
• More than one file may be moved at a time if the
destination is a directory:
• mv [options] file1 file2 destination
• Entire directory tree is moved
mv File Scenarios
• mv file1 to existing dir1
• file1 moved to dir1, same file name file1
• mv file1 to non-existing dir1/dir2
• file1 moved to directory dir1, renamed to file dir2
• mv file1 to existing file2 (invalid)
• mv file1 to non-existing file2
• file1 renamed as file2
mv Directory Scenarios
• mv dir1 to existing dir2
• dir1 moved to dir2, same dir name dir1
• mv dir1 to non-existing dir2/dir3
• dir1 moved to directory dir2, renamed to directory dir3
• mv dir1 to existing file1 (invalid)
• mv dir1 to non-existing file1
• dir1 renamed as directory file1
• mv dir1 to non-existing dir2/file1
• dir1 moved to directory dir2, renamed to directory file1
Creating and Removing Files
• touch <file>
• if <file> does not exist, create empty file
• if <file> exists, update its timestamps
• rm - remove files
Usage: rm [options] <file>...
• rm -i file (interactive)
• rm -r directory (recursive)
• rm -f file (force)
Creating and Removing Directories
• mkdir dir1 creates directory dir1
• mkdir –p dir1/dir2/dir3 creates all directories
• rmdir dir1 removes empty dir1
• dir1 cannot have files or directories
• rm -r dir1 removes dir1 and its entire directory tree
Wildcard Characters
• ? – match a single character
$ ls file?
$ ls file?.txt

• * – match multiple characters (any)


$ ls *
$ ls *.*
$ ls file*.txt
$ ls *.zip
File Globbing
• Globbing is wildcard expansion:
• * - matches zero or more characters
• ? - matches any single character
• [0-9] - matches a range of numbers. Here, from 0 to 9
• [abc] - matches any of the characters in the list. Here, a or b
or c
• [^abc] - matches all except the characters in the list. Here, all
but a, b or c
• Predefined character classes can be used

Globbing in Linux – How to use Wildcards (4:38)


Copy directory
• To copy a directory, use the –r (recursive) option
• To preserve the creation date, use –rp option
$ cp –r /etc/init . // copy init directory tree to current directory
$ cp –rp /etc/init . // copy directory to current directory,
preserve permission
Remove a directory tree
Usage:
• rm –r (recursive)
• rmdir –p (parents)
$ mkdir –p dir2/dir3 // create directory dir3 under dir2
$ mkdir –p dir3/dir4 // create directory dir4 under dir3
$ rm –rf dir2 // remove dir2 and dir3 directory under it
$ rmdir –p dir3/dir4 // remove dir3 and dir4 under it
which
which – show path to command
• Usage: which [options] command…

NOTE: Useful when “Command not found” error returned


whereis
whereis – show path to command and man chapter
• Usage: whereis [options] command…

NOTE: Useful to help fix bad PATH

$ whereis echo
echo: /bin/echo
/usr/share/man/man1/echo.1.gz
find
find – search a directory tree
• Usage: find <directory> [option]
[expression]
• –i (case insensitive)
• –name (filtered by pattern)
• –user | –uid (filtered by user or UID
• –group | –gid (filtered by group or GID)
• –size (filtered by size)
• –perm (filtered by permission)
• –atime | –mtime | –ctime (by access, mod or
create time)
• –exec | –ok (execute command on output)
The Power of the Linux Find Command (2:43)
find examples
$ find -name car // if has ‘car’ in its name
$ find -name Car // if has ‘Car’ in its name
$ find /tmp –user root // if owned by user=root
$ find /tmp –gid 500 // if owned by group with gid=500
$ find /user/share -size 1024k // if size is exactly 1 mb
$ find /user/share -size +1024k // if size is > 1 mb
$ find /user/share -size -1024k // if size is < 1 mb
$ find /tmp -perm 755 // if mode is exactly 755
$ find /tmp -perm +222 // if anyone can write
$ find /tmp -perm -222 // if no one can write
$ find /tmp -perm -222 // if ‘other’ cannot write
$ find /tmp -mtime -10 // if modified < 10 days ago
$ find /tmp -ctime +20 // if created > 20 days ago

Linux: Finding Files (3:40)


Text handling commands
• file – show file type
• cat – show file contents
• more – page file contents
• less – page file contents
• head – show start of file
• tail – show end of file
• grep – show lines matching a pattern
• cut – show selected columns
Introduction to Linux File System (6:52)
grep
grep – print lines matching a pattern
• Usage: grep [options] pattern <pathname>…
• -i (case insensitive matching)
• -v (exclude lines that match)
• -n (display line number)
• -AX (include X lines after pattern match)
• -BX (include X lines before pattern match)
grep examples
$ cat /etc/passwd // show passwd contents
$ grep root /etc/passwd // find lines having root
pattern
$ grep –n root /etc/passwd // this time show line
number too
$ grep –v root /etc/passwd // exclude lines having
root pattern
$ grep –A3 nobody /etc/passwd
// show lines having nobody pattern and the 3 lines
after
$ grep –B3 nobody /etc/passwd
// show
Grep and lines
Regexhaving
– BASH- nobody pattern and the 3 lines
Linux (8:24)
before
Text Analysis commands
• wc – count words in a file
• sort – sort lines of a file
• uniq – show unique lines of a file
• diff – show differences between files
• cmp – show differences between files

Uniq utility (4:45)


Text Manipulation commands
• tr – translate file
• sed – stream editor

Shell Episode 002 - tr command (14:00)


SED Tutorial Basic Substitution Linux Shell
BASHl (8:38)
wc (word count)
wc – print counts in file(s)
• Usage: wc [options] <pathname>…
• wc -l file (count # lines)
• wc -w file (count # words)
• wc -c file (count # bytes)

$ wc –l /etc/profile // show number of lines


$ wc –w /etc/profile // show number of words
$ wc –c /etc/profile // show number of bytes
$ wc /etc/profile // show number of lines, words,
bytes
Standard I/O and Pipes
Standard Input and Output
Linux provides three I/O channels to Programs
Name Default File number
STDIN Keyboard 0
STDOUT Console 1
STDERR Console 2
Default STDIN/STDOUT/STDERR
• At command prompt, bash running on the console
listens on STDIN for characters typed on the keyboard
• Command writes output to STDOUT which is the console
• Command writes error messages to STDERR which is
also the console
Redirecting Output to a File
command <operator> filename
• > redirect STDOUT to file (overwrite)
• >> redirect STDOUT to file (append)
• 2> redirect STDERR to file (overwrite)
• 2>&1 redirect STDERR to STDOUT
• &> redirects STDOUT and STDERR
• >/dev/null discards STDOUT output
• 2>/dev/null discards STDERR output
• &>/dev/null discards all output
Redirecting Linux Command Line Output to a File (5:
30)
Redirection Examples

• This command generates output and errors when run as


non-root:

• Operators can /etc


$ find be used
–nameto store output and errors
passwd
$ find /etc –name passwd > find.1.out
$ find /etc –name passwd 2> /dev/null
$ find /etc –name passwd > find.1.out
2>find.err
$ find /etc –name passwd > find.all.out
2>&1
Piping
• Redirect STDOUT to STDIN for another command
• Pipes (the | character) can connect commands:
command1 | command2
• Sends STDOUT of command1 to STDIN of command2 instead
of the screen.
• STDERR is not forwarded across pipes
• Used to combine the functionality of multiple tools
• command1 | command2 | command3... etc

Pipes, Grep, Sort – Linux Tutorial (5:57


)
Piping examples
// only show lines with man8
$ find /usr/share/man –name man8
// equivalent to above
$ find /usr/share/man | grep man8
// show lines containing man8 and user
$ find /usr/share/man | grep man8 | grep
user
// show lines containing man8 but excluding user
$ find /usr/share/man | grep man8 | grep –v
user
// show lines containing man8 but sorted
alphabetically
$ find /usr/share/man | grep man8 | sort
BASH shell
// show thecommands
numberpipes (9:04)
of lines containing man8

You might also like