Power Unix Commands

advertisement
CSIS 82
Tutorial: Powerful Unix Commands
Gagne Ch5
Commands to Practice and Master
tty
identify your workstation
echo
repeat a line to standard output
finger
identify a user and find out more about them
last
who last log in and are they still logged in
More Essential commands
cat
concatenate files (join together)
sort
sort the contents of a file or any output
uniq
return only the unique lines
wc
word count: return a count of words, characters and lines
Listing Options cd /etc to try out
ls –b Octel representation for filenames w/control characters
ls –ls Long listing sorted by file size
ls –lt Long listing sorted by time with the newest files at the top of the list
ls –a show hidden files and directories, often crackers/viruses will insert these
Listing using wildcards *= any number of characters , ? = one single character
ls *.txt
All .txt files
ls h*
All files beginning with h
ls as?.txt
would show as1.txt , as2.txt, asX.txt etc, but not as.txt or as10.txt
Removing files – permanently removes a file, cannot be recovered
rm –i
Ask before removing a file
rm -- -anotherfile
removes a filename beginning with – (-- Means no more options)
rm " onemorefile"
Removes a file name containing a leading space
Alias -- All root user should have
alias rm='rm –i'
query before removing
alias cp='cp –i'
query before copying on to an existing file
alias mv='mv –i'
query before moving or renaming on to an existing file
rm –f, mv –f, cp-f
force a remove/move/copy even if query option alias is set
alias
show what aliases you already have
unalias rm
undo an alias and recover the original command
Put aliases in .bashrc file in /root to make permanent
Redirecting standard input (<) And standard output (>) and append (>>)
cat
repeats what you type until you hit ^D
cat > names
puts what you type into a file called names
cat >> names
add a few more names to end of file names
cat < names
takes input from file names
cat file1 file2 file3
displays three files seamlessly
cat file1 file2 file3 > file4
merges 3 files into a fourth
sort
sorts what you type after you hit ^D
sort names
Sorts the contents of the names file
sort < names
takes input from file names
sort < names > sortednames A true "filter" between two files
ls –R / 1> allfiles 2>/dev/null
puts output in allfiles, sends errors to bitbucket
find / -name *.jpg 1> jpgfiles 2>/dev/null
Piping (|) chain several filter commands together
ls –l | more
do a long list 1 page at a time
cat names | sort | uniq > sorted_unique_names
plumbing between cmds
File Permissions
ls –l
shows file permission -rw-r--r-- (user has r/w, group & other r only)
touch test
makes a new empty file called test
ls –l test
should be -rw-r--r-chmod g+w test
turn on group write -rw-rw-r-- for file test
chmod u-w test
turn off write for user -r--r--r-chmod a=r test
all are read only
-r--r--r-chmod 700 test
rwx for user only
-rwx------ (4=read, 2=write, 1=execute, add)
chmod 536 test
-r-x-wxrwchmod
chown
chgrp
change (permission) mode of a file or directory
Change owner of file or a directory
Change group of a file or a directory
chmod –R 700 *
recursively change all files in all subdirectories of current directory
(see also setuid bit, Gagne, p60)
File Attributes for Advanced Protection
lsattr test
list attributes for a file
chattr +a test
test can only be appended to (root only)
chattr +i test
test is immutable, i.e. can't be modified, apply to critical system files (root
only)
chattr +s test
paranoia – file will be zeroed upon delete
Find and Grep Variations: find start_dir [options] , grep pattern file
find / -name "*.mpg" -print find all mpg video files on entire system
find /usr/sbin –size +1024 \
\( -mtime +365 –o –atime +365 \) –ls
(see Gagne, p64)
grep tux /etc/passwd
is there a user tux in my system?
grep -i -l -R 'hierarch' /home/mpc82/cs82 is there a file contains hierarch (ignore case)
Download