The Sysadmin Notebook  

Sitemap

Linux Commands

Notes on various commands for Linux

Contents

Compression

Top Bottom

zip/unzip

Top Bottom

list 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 dirname

tar

Top Bottom

extract 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/C2000

gzip

Top Bottom

Compress filename into filename.gz

gzip filename

Uncompress filename.gz to filename

gunzip filename.gz

Uncompress filename.gz to STDOUT

zcat filename.gz

bzcat

Top Bottom

Search for pattern in bzip file

bzcat archive.bz2 | grep pattern

Files and Directories

Top Bottom

deleting

Top Bottom

prompt before deleting all .doc files in current directory

rm -i *.doc

delete a file making it practically unrecoverable

shred -n100 -u -v -z topsecret.doc

list files

Top Bottom

List 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 Bottom

Print 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 Bottom

Rename banner01,02,..,0n.jpg files to header01,02,..,0n.jpg:

rename banner header *.jpg

rename files from .html to .tt2:

rename .html .tt2 *.html

grep

Top Bottom

Exclude 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 Bottom

Find 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 '{}' \; | sort

Networking

Top Bottom

ssh

Top Bottom

Synchronise 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@hostname

netstat

Top Bottom

Use 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 -aptu

lsof

Top Bottom

Use lsof to identify underlying application and PID by port:

lsof -i:5353

redirecting input and output

Top Bottom

run 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.txt

Subversion

Top Bottom

checkout 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 Bottom

The 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:

tidy

Top Bottom

Tidy 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 Bottom

Connect to an IRC channel

xchat --url=//irc.perl.org/catalyst

wget

Top Bottom

Collect all the css files from a website:

wget -r -nd -Acss http://home.clara.net/