System Information
arch Display machine processor architecture (1)
uname -m Display machine processor architecture (2)
uname -r Display the kernel version in use
dmidecode -q Display hardware system components – (SMBIOS / DMI)
hdparm -i /dev/hda List architectural features of a disk
hdparm -tT /dev/sda Perform a test read operation on the disk
cat /proc/cpuinfo Display CPU info
cat /proc/interrupts Display interrupts
cat /proc/meminfo Verify memory usage
cat /proc/swaps Show which swap files are in use
cat /proc/version Display kernel version
cat /proc/net/dev Display network adapters and statistics
cat /proc/mounts Display mounted filesystems
lspci -tv List PCI devices
lsusb -tv Show USB devices
date Display system date
cal 2007 Display the 2007 calendar
date 041217002007.00 Set date and time – MonthDayHourMinuteYear.Seconds
clock -w Save time modifications to BIOS
Shutdown (System Shutdown, Restart, and Logout)
shutdown -h now Shut down the system (1)
init 0 Shut down the system (2)
telinit 0 Shut down the system (3)
shutdown -h hours:minutes & Schedule system shutdown at a specific time
shutdown -c Cancel a scheduled system shutdown
shutdown -r now Restart (1)
reboot Restart (2)
logout Log out
Files and Directories
cd /home Enter the ‘/home’ directory
cd .. Go back to the parent directory
cd ../.. Go back to the parent’s parent directory
cd Enter the personal home directory
cd ~user1 Enter the personal home directory of user1
cd – Return to the last visited directory
pwd Show the current working directory path
ls View files in the directory
ls -F View files in the directory
ls -l Show detailed information of files and directories
ls -a Show hidden files
ls *[0-9]* Show filenames and directory names containing numbers
tree Display a tree structure of files and directories starting from root (1)
lstree Display a tree structure of files and directories starting from root (2)
mkdir dir1 Create a directory named ‘dir1’
mkdir dir1 dir2 Create two directories simultaneously
mkdir -p /tmp/dir1/dir2 Create a directory tree
rm -f file1 Delete a file named ‘file1’
rmdir dir1 Delete a directory named ‘dir1’
rm -rf dir1 Delete a directory named ‘dir1’ and all its contents recursively
rm -rf dir1 dir2 Delete two directories and their contents simultaneously
mv dir1 new_dir Rename/Move a directory
cp file1 file2 Copy a file
cp dir/* . Copy all files from a directory to the current working directory
cp -a /tmp/dir1 . Copy a directory to the current working directory
cp -a dir1 dir2 Copy a directory
ln -s file1 lnk1 Create a symbolic (soft) link pointing to a file or directory
ln file1 lnk1 Create a hard link pointing to a file or directory
touch -t 0712250000 file1 Modify the timestamp of a file or directory – (YYMMDDhhmm)
file file1 outputs the mime type of the file as text
iconv -l List known encodings
iconv -f fromEncoding -t toEncoding inputFile > outputFile creates a new from the given input file by assuming it is encoded in fromEncoding and converting it to toEncoding.
find . -maxdepth 1 -name *.jpg -print -exec convert "{}" -resize 80×60 "thumbs/{}" /; batch resize files in the current directory and send them to a thumbnails directory (requires convert from Imagemagick)
File Search
find / -name file1 Search for files and directories starting from the root filesystem ‘/’
find / -user user1 Search for files and directories belonging to user ‘user1’
find /home/user1 -name /*.bin Search for files ending with ‘.bin’ in directory ‘/home/user1’
find /usr/bin -type f -atime +100 Search for executable files not accessed in the past 100 days
find /usr/bin -type f -mtime -10 Search for files created or modified within the last 10 days
find / -name /*.rpm -exec chmod 755 ‘{}’ /; Search for files ending with ‘.rpm’ and set their permissions
find / -xdev -name /*.rpm Search for files ending with ‘.rpm’, ignoring removable devices like CD-ROM drives, USB drives, etc.
locate /*.ps Find files ending with ‘.ps’ – run ‘updatedb’ command first
whereis halt Show the location of a binary, source, or man page
which halt Show the full path of a binary or executable
Mounting a Filesystem
mount /dev/hda2 /mnt/hda2 Mount a drive called hda2 – ensure directory ‘/mnt/hda2’ exists
umount /dev/hda2 Unmount a drive called hda2 – ensure you exit from the mount point ‘/mnt/hda2’ first
fuser -km /mnt/hda2 Force unmount when the device is busy
umount -n /mnt/hda2 Perform unmount operation without writing to /etc/mtab – useful when the file is read-only or the disk is full
mount /dev/fd0 /mnt/floppy Mount a floppy disk
mount /dev/cdrom /mnt/cdrom Mount a CD-ROM or DVD-ROM
mount /dev/hdc /mnt/cdrecorder Mount a CD-RW or DVD-ROM
mount /dev/hdb /mnt/cdrecorder Mount a CD-RW or DVD-ROM
mount -o loop file.iso /mnt/cdrom Mount a file or ISO image file
mount -t vfat /dev/hda5 /mnt/hda5 Mount a Windows FAT32 filesystem
mount /dev/sda1 /mnt/usbdisk Mount a USB flash drive or thumb drive
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share Mount a Windows network share
Disk Space
df -h Display a list of mounted partitions
ls -lSr |more Arrange files and directories by size
du -sh dir1 Estimate disk space used by directory ‘dir1’
du -sk * | sort -rn Show file and directory sizes sorted by capacity
rpm -q -a –qf ‘%10{SIZE}t%{NAME}n’ | sort -k1,1n Show space used by installed RPM packages sorted by size (Fedora, Red Hat systems)
dpkg-query -W -f=’${Installed-Size;10}t${Package}n’ | sort -k1,1n Show space used by installed DEB packages sorted by size (Ubuntu, Debian systems)
Back to Top Index ^
Users and Groups
groupadd group_name Create a new user group
groupdel group_name Delete a user group
groupmod -n new_group_name old_group_name Rename a user group
useradd -c "Name Surname " -g admin -d /home/user1 -s /bin/bash user1 Create a user belonging to the "admin" group
useradd user1 Create a new user
userdel -r user1 Delete a user ( ‘-r’ removes the home directory)
usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 Modify user attributes
passwd Change password
passwd user1 Change a user’s password (root only)
chage -E 2005-12-31 user1 Set a user’s password expiration date
pwck Check the file format and syntax fixes of ‘/etc/passwd’ and verify existing users
grpck Check the file format and syntax fixes of ‘/etc/passwd’ and verify existing groups
newgrp group_name Log in to a new group to change the default group for newly created files
Back to Top Index ^
File Permissions – Use "+" to set permissions, "-" to revoke
ls -lh Display permissions
ls /tmp | pr -T5 -W$COLUMNS Split the terminal into 5 columns for display
chmod ugo+rwx directory1 Grant read (r), write (w), and execute (x) permissions to owner (u), group (g), and others (o) on a directory
chmod go-rwx directory1 Revoke read, write, and execute permissions for group (g) and others (o) on a directory
chown user1 file1 Change the owner of a file
chown -R user1 directory1 Change the owner of a directory and all files within it recursively
chgrp group1 file1 Change the group of a file
chown user1:group1 file1 Change the owner and group of a file
find / -perm -u+s List all files in a system using SUID control
chmod u+s /bin/file1 Set the SUID bit on a binary file – users running this file get the same permissions as the owner
chmod u-s /bin/file1 Disable the SUID bit on a binary file
chmod g+s /home/public Set the SGID bit on a directory – similar to SUID, but for directories
chmod g-s /home/public Disable the SGID bit on a directory
chmod o+t /home/public Set the STICKY bit on a file – only the legal owner can delete the file
chmod o-t /home/public Disable the STICKY bit on a directory
Back to Top Index ^
Special File Attributes – Use "+" to set attributes, "-" to revoke
chattr +a file1 Allow append-only write access to the file
chattr +c file1 Allow this file to be automatically compressed/decompressed by the kernel
chattr +d file1 The dump program will ignore this file during filesystem backup
chattr +i file1 Set as immutable; the file cannot be deleted, modified, renamed, or linked
chattr +s file1 Allow the file to be securely deleted
chattr +S file1 Once an application performs a write operation on this file, force the system to write modifications to disk immediately
chattr +u file1 If the file is deleted, the system will allow you to recover it laterDeleted Files
lsattr Show special attributes
Back to Top ^
Archiving and Compressing Files
bunzip2 file1.bz2 Decompress a file named 'file1.bz2'
bzip2 file1 Compress a file named 'file1'
gunzip file1.gz Decompress a file named 'file1.gz'
gzip file1 Compress a file named 'file1'
gzip -9 file1 Compress with maximum compression
rar a file1.rar test_file Create an archive named 'file1.rar'
rar a file1.rar file1 file2 dir1 Compress 'file1', 'file2', and directory 'dir1' simultaneously
rar x file1.rar Extract a rar archive
unrar x file1.rar Extract a rar archive
tar -cvf archive.tar file1 Create an uncompressed tarball
tar -cvf archive.tar file1 file2 dir1 Create an archive containing 'file1', 'file2', and 'dir1'
tar -tf archive.tar Display the contents of an archive
tar -xvf archive.tar Extract an archive
tar -xvf archive.tar -C /tmp Extract an archive to the /tmp directory
tar -cvfj archive.tar.bz2 dir1 Create a bzip2 format compressed archive
tar -xvfj archive.tar.bz2 Decompress a bzip2 format compressed archive
tar -cvfz archive.tar.gz dir1 Create a gzip format compressed archive
tar -xvfz archive.tar.gz Decompress a gzip format compressed archive
zip file1.zip file1 Create a zip format compressed archive
zip -r file1.zip file1 file2 dir1 Compress several files and directories into a single zip format archive
unzip file1.zip Decompress a zip format archive
Back to Top ^
RPM Packages – (Fedora, Redhat and similar systems)
rpm -ivh package.rpm Install an rpm package
rpm -ivh –nodeeps package.rpm Install an rpm package while ignoring dependency warnings
rpm -U package.rpm Update an rpm package without changing its configuration files
rpm -F package.rpm Update an rpm package that is definitely already installed
rpm -e package_name.rpm Remove an rpm package
rpm -qa Show all rpm packages installed in the system
rpm -qa | grep httpd Show all rpm packages whose name contains "httpd"
rpm -qi package_name Get specific information for an installed package
rpm -qg "System Environment/Daemons" Show rpm packages for a component group
rpm -ql package_name Show the list of files provided by an installed rpm package
rpm -qc package_name Show the list of configuration files provided by an installed rpm package
rpm -q package_name –whatrequires Show a list of packages that depend on an rpm package
rpm -q package_name –whatprovides Show the size an rpm package occupies
rpm -q package_name –scripts Show scripts executed during installation/removal
rpm -q package_name –changelog Show the modification history of an rpm package
rpm -qf /etc/httpd/conf/httpd.conf Confirm which rpm package provides the given file
rpm -qp package.rpm -l Show the list of files provided by an rpm package not yet installed
rpm –import /media/cdrom/RPM-GPG-KEY Import public key digital certificates
rpm –checksig package.rpm Confirm the integrity of an rpm package
rpm -qa gpg-pubkey Confirm the integrity of all installed rpm packages
rpm -V package_name Check file size, permissions, type, owner, group, MD5 checksum, and last modification time
rpm -Va Check all installed rpm packages on the system – use with caution
rpm -Vp package.rpm Confirm an rpm package is not yet installed
rpm2cpio package.rpm | cpio –extract –make-directories *bin* Run executable files from an rpm package
rpm -ivh /usr/src/redhat/RPMS/`arch`/package.rpm Install a built package from an rpm source
rpmbuild –rebuild package_name.src.rpm Build an rpm package from an rpm source
Back to Top ^
YUM Package Updater – (Fedora, RedHat and similar systems)
yum install package_name Download and install an rpm package
yum localinstall package_name.rpm Install an rpm package, using your own software repository to resolve all dependencies
yum update package_name.rpm Update all currently installed rpm packages in the system
yum update package_name Update an rpm package
yum remove package_name Remove an rpm package
yum list List all packages installed in the current system
yum search package_name Search for packages in the rpm repository
yum clean packages Clean the rpm cache, deleting downloaded packages
yum clean headers Delete all header files
yum clean all Delete all cached packages and header files
Back to Top ^
DEB Packages (Debian, Ubuntu and similar systems)
dpkg -i package.deb Install/Update a deb package
dpkg -r package_name Remove a deb package from the system
dpkg -l Show all deb packages installed in the system
dpkg -l | grep httpd Show all deb packages whose name contains "httpd"
dpkg -s package_name Get information on a specific package installed in the system
dpkg -L package_name Show the list of files provided by an installed deb package on the system
dpkg –contents package.deb Show the list of files provided by a package not yet installed
dpkg -S /bin/ping Confirm which deb package provides the given file
Back to Top ^
APT Software Tools (Debian, Ubuntu and similar systems)
apt-get install package_name Install/Update a deb package
apt-cdrom install package_name Install/Update a deb package from CD-ROM
apt-get update Upgrade the package list
apt-get upgrade Upgrade all installed software
apt-get remove package_name Remove a deb package from the system
apt-get check Confirm the dependency repository is correct
apt-get clean Clean the cache from downloaded packages
apt-cache search searched-package Return package names containing the searched string
Back to Top ^
Viewing File Content
cat file1 View file content forward from the first byte
tac file1 View file content backwards starting from the last line
more file1 View the content of a long file
less file1 Similar to the 'more' command, but allows reverse operation just like forward operation within the file
head -2 file1 View the first two lines of a file
tail -2 file1 View the last two lines of a file
tail -f /var/log/messages View content being appended to a file in real-time
Back to Top ^
Text Processing
cat file1 file2 … | command <> file1_in.txt_or_file1_out.txt general syntax for text manipulation using PIPE, STDIN and STDOUT
cat file1 | command( sed, grep, awk, grep, etc…) > result.txt Combine detailed description text of a file and write the summary into a new file
cat file1 | command( sed, grep, awk, grep, etc…) >> result.txt Combine detailed description text of a file and append the summary into an existing file
grep Aug /var/log/messages Search for the keyword "Aug" in the file '/var/log/messages'
grep ^Aug /var/log/messages Search for words starting with "Aug" in the file '/var/log/messages'
grep [0-9] /var/log/messages Select all lines containing numbers in the '/var/log/messages' file
grep Aug -R /var/log/* Search for the string "Aug" in the directory '/var/log' and subsequent directories
sed 's/stringa1/stringa2/g' example.txt Replace "string1" with "string2" in the example.txt file
sed '/^$/d' example.txt Delete all blank lines from the example.txt file
sed '/ *#/d; /^$/d' example.txt Delete all comments and blank lines from the example.txt file
echo 'esempio' | tr '[:lower:]' '[:upper:]' Merge upper and lower case cell content
sed -e '1d' result.txt Exclude the first line from the file example.txt
sed -n '/stringa1/p' View only lines containing the word "string1"
sed -e 's/ *$//' example.txt Delete trailing whitespace characters from each line
sed -e 's/stringa1//g' example.txt Remove only the word "string1" from the document and keep all the rest
sed -n '1,5p;5q' example.txt View content from line 1 to line 5
sed -n '5p;5q' example.txt View line 5
sed -e 's/00*/0/g' example.txt Replace multiple zeros with a single zero
cat -n file1 Number the lines of a file
cat example.txt | awk 'NR%2==1' Delete all even-numbered lines from the example.txt file
echo a b c | awk '{print $1}' View the first column of a line
echo a b c | awk '{print $1,$3}' View the first and third columns of a line
paste file1 file2 Merge the contents of two files or columns
paste -d '+' file1 file2 Merge the contents of two files or columns, separated by "+"
sort file1 file2 Sort the contents of two files
sort file1 file2 | uniq Get the union of two files (duplicate lines are kept only once)
sort file1 file2 | uniq -u Delete the intersection, leaving the other lines
sort file1 file2 | uniq -d Get the intersection of two files (onlyKeep lines present in both files)
comm -1 file1 file2 Compare two files, removing content unique to ‘file1’
comm -2 file1 file2 Compare two files, removing content unique to ‘file2’
comm -3 file1 file2 Compare two files, removing common content shared by both files
Back to Top Index ^
Character Set and File Format Conversion
dos2unix filedos.txt fileunix.txt Convert a text file format from MSDOS to UNIX
unix2dos fileunix.txt filedos.txt Convert a text file format from UNIX to MSDOS
recode ..HTML < page.txt > page.html Convert a text file to HTML
recode -l | more Display all allowed conversion formats
Back to Top Index ^
Filesystem Analysis
badblocks -v /dev/hda1 Check for bad blocks on disk hda1
fsck /dev/hda1 Repair/check the integrity of the Linux filesystem on disk hda1
fsck.ext2 /dev/hda1 Repair/check the integrity of the ext2 filesystem on disk hda1
e2fsck /dev/hda1 Repair/check the integrity of the ext2 filesystem on disk hda1
e2fsck -j /dev/hda1 Repair/check the integrity of the ext3 filesystem on disk hda1
fsck.ext3 /dev/hda1 Repair/check the integrity of the ext3 filesystem on disk hda1
fsck.vfat /dev/hda1 Repair/check the integrity of the FAT filesystem on disk hda1
fsck.msdos /dev/hda1 Repair/check the integrity of the DOS filesystem on disk hda1
dosfsck /dev/hda1 Repair/check the integrity of the DOS filesystem on disk hda1
Back to Top Index ^
Initialize a Filesystem
mkfs /dev/hda1 Create a filesystem on the hda1 partition
mke2fs /dev/hda1 Create a Linux ext2 filesystem on the hda1 partition
mke2fs -j /dev/hda1 Create a Linux ext3 (journaled) filesystem on the hda1 partition
mkfs -t vfat 32 -F /dev/hda1 Create a FAT32 filesystem
fdformat -n /dev/fd0 Format a floppy disk
mkswap /dev/hda3 Create a swap filesystem
Back to Top Index ^
SWAP Filesystem
mkswap /dev/hda3 Create a swap filesystem
swapon /dev/hda3 Enable a new swap filesystem
swapon /dev/hda2 /dev/hdb3 Enable two swap partitions
Back to Top Index ^
Backup
dump -0aj -f /tmp/home0.bak /home Make a full backup of the ‘/home’ directory
dump -1aj -f /tmp/home0.bak /home Make an incremental backup of the ‘/home’ directory
restore -if /tmp/home0.bak Restore an incremental backup
rsync -rogpav –delete /home /tmp Sync directories on both sides
rsync -rogpav -e ssh –delete /home ip_address:/tmp Rsync via SSH tunnel
rsync -az -e ssh –delete ip_addr:/home/public /home/local Sync a remote directory to a local directory via SSH with compression
rsync -az -e ssh –delete /home/local ip_addr:/home/public Sync a local directory to a remote directory via SSH with compression
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr ‘dd of=hda.gz’ Perform a backup of a local disk on a remote host via SSH
dd if=/dev/sda of=/tmp/file1 Backup disk content to a file
tar -Puf backup.tar /home/user Perform an incremental backup of the ‘/home/user’ directory
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr ‘cd /home/share/ && tar x -p’ Copy directory content to a remote directory via SSH
( tar c /home ) | ssh -C user@ip_addr ‘cd /home/backup-home && tar x -p’ Copy a local directory to a remote directory via SSH
tar cf – . | (cd /tmp/backup ; tar xf – ) Locally copy one directory to another, preserving original permissions and links
find /home/user1 -name ‘*.txt’ | xargs cp -av –target-directory=/home/backup/ –parents Find and copy all files ending with ‘.txt’ from one directory to another
find /var/log -name ‘*.log’ | tar cv –files-from=- | bzip2 > log.tar.bz2 Find all files ending with ‘.log’ and create a bzip archive
dd if=/dev/hda of=/dev/fd0 bs=512 count=1 Copy the MBR (Master Boot Record) content to a floppy disk
dd if=/dev/fd0 of=/dev/hda bs=512 count=1 Restore MBR content from a backup saved to floppy disk
Back to Top Index ^
Optical Disc (CD/DVD)
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force Erase a rewritable disc
mkisofs /dev/cdrom > cd.iso Create an ISO image of a disc on the hard drive
mkisofs /dev/cdrom | gzip > cd_iso.gz Create a compressed ISO image of a disc on the hard drive
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4 -o ./cd.iso data_cd Create an ISO image from a directory
cdrecord -v dev=/dev/cdrom cd.iso Burn an ISO image
gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom – Burn a compressed ISO image
mount -o loop cd.iso /mnt/iso Mount an ISO image
cd-paranoia -B Rip audio tracks from a CD to WAV files
cd-paranoia — "-3" Rip audio tracks from a CD to WAV files (parameter -3)
cdrecord –scanbus Scan bus to identify SCSI channels
dd if=/dev/hdc | md5sum Verify the md5sum checksum of a device, e.g., a CD
Back to Top Index ^
Network – (Ethernet and WiFi Wireless)
ifconfig eth0 Display the configuration of an Ethernet card
ifup eth0 Enable the ‘eth0’ network device
ifdown eth0 Disable the ‘eth0’ network device
ifconfig eth0 192.168.1.1 netmask 255.255.255.0 Configure IP address
ifconfig eth0 promisc Set ‘eth0’ to promiscuous mode to sniff packets
dhclient eth0 Enable ‘eth0’ in DHCP mode
route -n show routing table
route add -net 0/0 gw IP_Gateway configura default gateway
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 configure static route to reach network ‘192.168.0.0/16’
route del 0/0 gw IP_gateway remove static route
echo "1" > /proc/sys/net/ipv4/ip_forward activate ip routing
hostname show hostname of system
host www.example.com lookup hostname to resolve name to ip address and viceversa(1)
nslookup www.example.com lookup hostname to resolve name to ip address and viceversa(2)
ip link show show link status of all interfaces
mii-tool eth0 show link status of ‘eth0’
ethtool eth0 show statistics of network card ‘eth0’
netstat -tup show all active network connections and their PID
netstat -tupl show all network services listening on the system and their PID
tcpdump tcp port 80 show all HTTP traffic
iwlist scan show wireless networks
iwconfig eth1 show configuration of a wireless network card
hostname show hostname
host www.example.com lookup hostname to resolve name to ip address and viceversa
nslookup www.example.com lookup hostname to resolve name to ip address and viceversa
whois www.example.com lookup on Whois database
GO TOP INDEX ^
Microsoft Windows networks (SAMBA)
nbtscan ip_addr netbios name resolution
nmblookup -A ip_addr netbios name resolution
smbclient -L ip_addr/hostname show remote shares of a windows host
smbget -Rr smb://ip_addr/share like wget can download files from a host windows via smb
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share mount a windows network share