Linux Commands
Notes on various commands for Linux
Contents
Compression
Top Bottomzip/unzip
Top Bottomlist files in pictures.zip:
unzip -l pictures.zip
extract picture01.bmp from pictures.zip:
unzip pictures.zip picture01.bmp
extract all files from pictures.zip:
unzip pictures.zip
Create archive of all .bmp files in directory:
zip pictures.zip *.bmp
Recursively create archive of all files and directories in dirname:
zip archive.zip dirnametar
Top Bottomextract foo.tar:
tar -xf foo.tar
extract foo.tar.gz:
tar -xzf foo.tar.gz
extract foo.tar.bz2:
tar -xjf foo.tar.bz2
extract file.txt from foo.tar.gz
tar -xzf foo.tar.gz file.txt
create bzipped tar archive of directory bar called foo.tar.bz2:
tar -cjf foo.tar.bz2 bar/
create archive of .ttreerc and websrc/ in ttsite.tar.bz2:
tar -cjf ttsite.tar.bz2 .ttreerc websrc/
list files in ttsite.tar.bz2:
tar -tf ttsite.tar.bz2
extract directory data/vmware/C2000 and contents from machines.tar.bz2:
tar -xjf machines.tar.bz2 data/vmware/C2000gzip
Top BottomCompress filename into filename.gz
gzip filename
Uncompress filename.gz to filename
gunzip filename.gz
Uncompress filename.gz to STDOUT
zcat filename.gzbzcat
Top BottomSearch for pattern in bzip file
bzcat archive.bz2 | grep patternFiles and Directories
Top Bottomdeleting
Top Bottomprompt before deleting all .doc files in current directory
rm -i *.doc
delete a file making it practically unrecoverable
shred -n100 -u -v -z topsecret.doclist files
Top BottomList files sorted by size, largest first
ls -lS *
List files sorted by size, smallest first
ls -lSr *
List files sorted by modified time
ls -lt *cut
Top BottomPrint first character on each line in filename:
cut -c1 filename
Print first five characters on each line in filename:
cut -c1-5 filename
Print fifth field in comma delimited file:
cut -d, -f5 filename
Use 'stat' for information about a files attributes. 'file' for information about the file type.
rename
Top BottomRename banner01,02,..,0n.jpg files to header01,02,..,0n.jpg:
rename banner header *.jpg
rename files from .html to .tt2:
rename .html .tt2 *.htmlgrep
Top BottomExclude a pattern from search:
grep -v 'pattern' filename
Search for pattern recursively, printing line number in file:
grep 'pattern' directory -r -n
Search for pattern recursively, printing filename only if found:
grep 'pattern' directory -r -l
Search for pattern recursively, printing filename only if not found:
grep 'pattern' directory -r -L find
Top BottomFind all files and directories named index.html in myweb directory and subdirectories
find ~/myweb -name index.html
Find all files and directories with SQL in their name in the myweb directory and subdirectories
find ~/myweb -name *SQL*
Find only files with SQL in their name
find ~/myweb -name *SQL* -type f
Find only directories with SQL in their name
find ~/myweb -name *SQL* -type d
Find files named *.tt2 and execute 'stat' command
find ~/myweb -name *.tt2 -type f -exec stat '{}' \;
Find files ending with '~' and prompt to execute 'rm' command
find ~/myweb -name '*~' -type f -ok rm '{}' \;
Count all .html files in myweb directory
find ~/myweb -name *.html | wc -l
List all .html files in alphabetical order by filename
find ~/myweb -name *.html -exec basename '{}' \; | sortNetworking
Top Bottomssh
Top BottomSynchronise files from /home/dr00 on remote machine to /home/dr00 on local machine:
rsync -avz 192.168.1.5:/home/dr00/ /home/dr00
Synchronise multiple directories from /home/dr00 to another path. Creates each destination directory on the source:
rsync -avz /home/dr00/myweb /home/dr00/Pictures \
/home/dr00/Music /home/dr00/Videos /media/disk/
use ssh to run remote X11 programs:
ssh -Y user@hostnamenetstat
Top BottomUse netstat to identify underlying application and PID per port:
netstat -anpt
Use netstat to identify listening udp and tcp ports:
netstat -utln
Use netstat to identify connected udp and tcp ports:
netstat -ptu
Use netstat to identify listening and connected udp and tcp ports:
netstat -aptulsof
Top BottomUse lsof to identify underlying application and PID by port:
lsof -i:5353redirecting input and output
Top Bottomrun command and redirect output and errors to different files:
cmd 1>out.txt 2>err.txt
run command and redirect output and errors to same file:
cmd 1>out.txt 2>&1
run command, leave STDOUT and STDIN untouched, but capture all output to a file:
cmd | tee out.txtSubversion
Top Bottomcheckout the code for Authentication part of the Catalyst Tutorial:
svn co http://dev.catalyst.perl.org/repos/Catalyst/tags/examples/Tutorial/MyApp/5.7/Authentication MyApp Cron
Top BottomThe MAILTO environment variable can be set to either a local account, or a valid email address. To send to multiple emails, separate the addresses with a comma
MAILTO=user1@abc.com,user2@abc.com
Crontab entries take the form: min hour monthday month weekday 'command'. Weekdays can be specified as numbers or 3-digit abbreviations: 0 to 6 or sun to sat. A percent sign in the command will be replace with a new line. Examples:
- Run every five minutes: */5 1-23 * * * command
- Run every three hours: * */3 * * * command
- Run five minutes past every hour: 5 0-23 * * * command
- Run every three hours from 1:00am; 0 1,4,7,10,13,16,19,22 * * * command
- Run every three hours from 1:00am; 0 1-23/3 * * * command
- Run at midnight on the 5th of each month: 0 0 5 * * command
- Run at midnight on the first Monday of every month: not possible
- Run every monday at 3:30pm: 30 15 * * 1 command
- Run at midnight at the end of each financial quarter: 0 0 0 1,4,7,10 command
- Run on Christmas Day: 0 0 25 12 * mail santa@north.pole.com%Happy Birthday!%Many Happy Returns, Big Boy.
tidy
Top BottomTidy up sloppy html
tidy index.htm > index.html
List errors and warnings for html file
tidy -eq index.htm
List errors and warnings for directory tree of html files
find myweb/html -name '*.html' -print -exec tidy -eq '{}' \;"xchat
Top BottomConnect to an IRC channel
xchat --url=//irc.perl.org/catalystwget
Top BottomCollect all the css files from a website:
wget -r -nd -Acss http://home.clara.net/
