System Information
arch # Display the processor architecture of the machine (1)
uname -m # Display the processor architecture of the machine (2)
uname -r # Display the current kernel version
dmidecode -q # Display hardware system components - (SMBIOS / DMI)
hdparm -i /dev/hda # List the architectural features of a disk
hdparm -tT /dev/sda # Perform test read operations on the disk
cat /proc/cpuinfo # Display CPU information
cat /proc/interrupts # Display interrupts
cat /proc/meminfo # Check memory usage
cat /proc/swaps # Display which swaps are being used
cat /proc/version # Display the kernel version
cat /proc/net/dev # Display network adapters and statistics
cat /proc/mounts # Display loaded file systems
lspci -tv # List PCI devices
lsusb -tv # Display USB devices
Date Display System Date
cal 2007 # Display the calendar for the year 2007
date 041217002007.00 # Set date and time - month day hour minute year.second
clock -w # Modify and save time 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 & # Shut down the system at a scheduled time
shutdown -c # Cancel scheduled shutdown
shutdown -r now # Restart (1)
reboot # Restart (2)
logout # Logout
Files and Directories
cd /home # Enter the '/home' directory
cd .. # Go back to the previous directory
cd ../.. # Go back two directories
cd # Enter the user's home directory
cd ~user1 # Enter the home directory of user1
cd - # Go back to the last directory
pwd # Display the working path
ls # View files in the directory
ls -F # View files in the directory
ls -l # Display detailed information about files and directories
ls -a # Display hidden files
ls *[0-9]* # Display files and directories with numbers in their names
tree # Display the tree structure of files and directories starting from the root (1)
lstree # Display the tree structure of files and directories starting from the root (2)
mkdir dir1 # Create a directory called 'dir1'
mkdir dir1 dir2 # Create two directories simultaneously
mkdir -p /tmp/dir1/dir2 # Create a directory tree
rm -f file1 # Delete a file called 'file1'
rmdir dir1 # Delete a directory called 'dir1'
rm -rf dir1 # Delete a directory called 'dir1' and its contents
rm -rf dir1 dir2 # Simultaneously delete two directories and their contents
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 link to a file or directory
ln file1 lnk1 # Create a hard link to a file or directory
touch -t 0712250000 file1 # Modify a file or directory timestamp - (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 80x60 "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 '/'
find / -user user1 # Search for files and directories belonging to user 'user1'
find /home/user1 -name \*.bin # Search for files ending with '.bin' in the '/home/user1' directory
find /usr/bin -type f -atime +100 # Search for executable files that have not been used in the last 100 days
find /usr/bin -type f -mtime -10 # Search for files created or modified in 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 CDs and USB drives
locate \*.ps # Find files ending with '.ps' - first run 'updatedb' command
whereis halt # Display the location of a binary file, source code, or man page
which halt # Display the full path of a binary file or executable
Mounting a File System
mount /dev/hda2 /mnt/hda2 # Mount a disk called hda2 - ensure the directory '/mnt/hda2' exists
umount /dev/hda2 # Unmount a disk called hda2 - first exit the mount point '/mnt/hda2'
fuser -km /mnt/hda2 # Force unmount when the device is busy
umount -n /mnt/hda2 # Run unmount operation without writing to /etc/mtab - very useful when the file is read-only or when the disk is full
mount /dev/fd0 /mnt/floppy # Mount a floppy disk
mount /dev/cdrom /mnt/cdrom # Mount a cdrom or dvdrom
mount /dev/hdc /mnt/cdrecorder # Mount a cdrw or dvdrom
mount /dev/hdb /mnt/cdrecorder # Mount a cdrw or dvdrom
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 file system
mount /dev/sda1 /mnt/usbdisk # Mount a USB drive or flash device
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share # Mount a Windows network share
Disk Space
df -h # Display the list of mounted partitions
ls -lSr |more # Sort files and directories by size
du -sh dir1 # Estimate the disk space used by directory 'dir1'
du -sk * | sort -rn # Display the sizes of files and directories in descending order by capacity
rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n
# Display the space used by installed rpm packages in order of size (fedora, redhat systems)
dpkg-query -W -f='${Installed-Size;10}t${Package}n' | sort -k1,1n
# Display the space used by installed deb packages in order of size (ubuntu, debian systems)
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" user group
useradd user1 # Create a new user
userdel -r user1 # Delete a user ('-r' excludes 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 (only allowed for root)
chage -E 2005-12-31 user1 # Set the expiration date for a user's password
pwck # Check the format and syntax of the '/etc/passwd' file and correct existing users
grpck # Check the format and syntax of the '/etc/passwd' file and correct existing groups
newgrp group_name # Log into a new group to change the default group for newly created files
File Permissions
Use "+" to set permissions and "-" to revoke
ls -lh # Display permissions
ls /tmp | pr -T5 -W$COLUMNS # Divide the terminal into 5 columns
chmod ugo+rwx directory1 # Set read (r), write (w), and execute (x) permissions for owner (u), group (g), and others (o) on the directory
chmod go-rwx directory1 # Remove read, write, and execute permissions for group (g) and others (o) on the 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
chgrp group1 file1 # Change the group of a file
chown user1:group1 file1 # Change both the owner and group attributes of a file
find / -perm -u+s # List all files on the system that have SUID control
chmod u+s /bin/file1 # Set the SUID bit for a binary file - the user running this file gets the same permissions as the owner
chmod u-s /bin/file1 # Disable the SUID bit for a binary file
chmod g+s /home/public # Set the SGID bit for a directory - similar to SUID, but for directories
chmod g-s /home/public # Disable the SGID bit for a directory
chmod o+t /home/public # Set the STIKY bit for a file - only the legitimate owner can delete the file
chmod o-t /home/public # Disable the STIKY bit for a directory
File Special Attributes
- Use "+" to set permissions and "-" to revoke
chattr +a file1 # Allow the file to be read and written only in append mode
chattr +c file1 # Allow this file to be automatically compressed/decompressed by the kernel
chattr +d file1 # Ignore this file during filesystem backups by the dump program
chattr +i file1 # Set the file as immutable, cannot be deleted, modified, renamed, or linked
chattr +s file1 # Allow a file to be deleted securely
chattr +S file1 # Immediately write modifications to disk when an application writes to this file
chattr +u file1 # If the file is deleted, the system will allow you to recover this deleted file later
lsattr # Display special attributes
Packaging and Compressing Files
bunzip2 file1.bz2 # Decompress a file called 'file1.bz2'
bzip2 file1 # Compress a file called 'file1'
gunzip file1.gz # Decompress a file called 'file1.gz'
gzip file1 # Compress a file called 'file1'
gzip -9 file1 # Maximum compression
rar a file1.rar test_file # Create a package called 'file1.rar'ar a file1.rar file1 file2 dir1 # Simultaneously compress 'file1', 'file2', and directory 'dir1'
ar x file1.rar # Decompress rar package
unrar x file1.rar # Decompress rar package
tar -cvf archive.tar file1 # Create a non-compressed tarball
tar -cvf archive.tar file1 file2 dir1 # Create an archive file containing 'file1', 'file2', and 'dir1'
tar -tf archive.tar # Display contents of a package
tar -xvf archive.tar # Extract a package
tar -xvf archive.tar -C /tmp # Extract the compressed package to /tmp directory
tar -cvfj archive.tar.bz2 dir1 # Create a bzip2 format compressed package
tar -jxvf archive.tar.bz2 # Decompress a bzip2 format compressed package
tar -cvfz archive.tar.gz dir1 # Create a gzip format compressed package
tar -zxvf archive.tar.gz # Decompress a gzip format compressed package
zip file1.zip file1 # Create a zip format compressed package
zip -r file1.zip file1 file2 dir1 # Compress several files and directories into a zip format compressed package
unzip file1.zip # Decompress a zip format compressed package
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 a confirmed installed rpm package
rpm -e package_name.rpm # Delete an rpm package
rpm -qa # Display all installed rpm packages in the system
rpm -qa | grep httpd # Display all rpm packages that contain the word "httpd" in their names
rpm -qi package_name # Get special information about an installed package
rpm -qg "System Environment/Daemons" # Display the rpm package of a component
rpm -ql package_name # Display the list of files provided by an installed rpm package
rpm -qc package_name # Display the list of configuration files provided by an installed rpm package
rpm -q package_name --whatrequires # Display a list of packages that have dependencies on an rpm package
rpm -q package_name --whatprovides # Display the size occupied by an rpm package
rpm -q package_name --scripts # Display the scripts executed during installation/deletion
rpm -q package_name --changelog # Display the change 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 # Display the list of files provided by an uninstalled rpm package
rpm --import /media/cdrom/RPM-GPG-KEY # Import public key digital certificate
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 check, and last modified time
rpm -Va # Check all installed rpm packages in the system - use with caution
rpm -Vp package.rpm # Confirm an rpm package that has not been 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
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, resolving all dependencies using your own software repository
yum update package_name.rpm # Update all installed rpm packages in the current system
yum update package_name # Update an rpm package
yum remove package_name # Delete an rpm package
yum list # List all packages currently installed in the system
yum search package_name # Search for packages in the rpm repository
yum clean packages # Clean up rpm cache and delete downloaded packages
yum clean headers # Delete all header files
yum clean all # Delete all cached packages and header files
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 # Display all installed deb packages in the system
dpkg -l | grep httpd # Display all deb packages that contain the word "httpd" in their names
dpkg -s package_name # Get information about a specific package installed on the system
dpkg -L package_name # Display the list of files provided by an installed deb package
dpkg --contents package.deb # Display the list of files provided by a package that has not been installed
dpkg -S /bin/ping # Confirm which deb package provides the given file
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 the CD
apt-get update # Upgrade packages listed in the list
apt-get upgrade # Upgrade all installed software
apt-get remove package_name # Remove a deb package from the system
apt-get check # Confirm that the dependencies of the software repository are correct
apt-get clean # Clean the cache of downloaded software packages
apt-cache search searched-package # Return the names of software packages containing the search string
View File Contents
cat file1 # View the contents of the file from the first byte
tac file1 # View the contents of a file from the last line
more file1 # View the contents of a long file
less file1 # Similar to 'more' command, but allows backward navigation in the file as well as forward
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 # Real-time view of content being added to a file
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
# Merge detailed description text of a file and write the summary to a new file
cat file1 | command( sed, grep, awk, grep, etc...) >> result.txt
# Merge detailed description text of a file and write the summary to 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 in the '/var/log/messages' file that contain numbers
grep Aug -R /var/log/* # Search for the string "Aug" in the '/var/log' directory 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 content of upper and lower cells
sed -e '1d' result.txt # Exclude the first line from the example.txt file
sed -n '/stringa1/p' # View only the lines containing the word "string1"
sed -e 's/ *$//' example.txt # Remove trailing whitespace from each line
sed -e 's/stringa1//g' example.txt
# Only delete the word "string1" from the document and keep the rest
sed -n '1,5p;5q' example.txt # View content from the first to the fifth line
sed -n '5p;5q' example.txt # View the fifth line
sed -e 's/00*/0/g' example.txt # Replace multiple zeros with a single zero
cat -n file1 # Mark the line numbers of the file
cat example.txt | awk 'NR%2==1' # Delete all even 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 two columns
paste -d '+' file1 file2 # Merge the contents of two files or two columns, separated by "+"
sort file1 file2 # Sort the contents of two files
sort file1 file2 | uniq # Take the union of two files (only keep one copy of duplicate lines)
sort file1 file2 | uniq -u # Delete the intersection, leaving other lines
sort file1 file2 | uniq -d # Take the intersection of two files (only keep lines that exist in both files)
comm -1 file1 file2 # Compare the contents of two files and only delete the contents contained in 'file1'
comm -2 file1 file2 # Compare the contents of two files and only delete the contents contained in 'file2'
comm -3 file1 file2 # Compare the contents of two files and only delete the parts common to both files
Character Settings 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
File System Analysis
badblocks -v /dev/hda1 # Check for bad blocks on disk hda1
fsck /dev/hda1 # Repair/check the integrity of the linux file system on disk hda1
fsck.ext2 /dev/hda1 # Repair/check the integrity of the ext2 file system on disk hda1
e2fsck /dev/hda1 # Repair/check the integrity of the ext2 file system on disk hda1
e2fsck -j /dev/hda1 # Repair/check the integrity of the ext3 file system on disk hda1
fsck.ext3 /dev/hda1 # Repair/check the integrity of the ext3 file system on disk hda1
fsck.vfat /dev/hda1 # Repair/check the integrity of the fat file system on disk hda1
fsck.msdos /dev/hda1 # Repair/check the integrity of the dos file system on disk hda1
dosfsck /dev/hda1 # Repair/check the integrity of the dos file system on disk hda1
Initialize a File System
mkfs /dev/hda1 # Create a file system on partition hda1
mke2fs /dev/hda1 # Create a linux ext2 file system on partition hda1
mke2fs -j /dev/hda1 # Create a linux ext3 (journaled) file system on partition hda1
mkfs -t vfat 32 -F /dev/hda1 # Create a FAT32 file system
fdformat -n /dev/fd0 # Format a floppy disk
mkswap /dev/hda3 # Create a swap file system
SWAP File System
mkswap /dev/hda3 # Create a swap file system
swapon /dev/hda3 # Enable a new swap file system
swapon /dev/hda2 /dev/hdb3 # Enable two swap partitions
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 interactive backup of the '/home' directory
restore -if /tmp/home0.bak # Restore an interactive backup
rsync -rogpav --delete /home /tmp # Synchronize two directories
rsync -rogpav -e ssh --delete /home ip_address:/tmp # Rsync through SSH
rsync -az -e ssh --delete ip_addr:/home/public /home/local # Synchronize a remote directory to a local directory via ssh and compression
rsync -az -e ssh --delete /home/local ip_addr:/home/public # Synchronize a local directory to a remote directory via ssh and compression
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' # Backup the local disk content remotely via ssh
dd if=/dev/sda of=/tmp/file1 # Backup disk content to a file
tar -Puf backup.tar /home/user # Perform an interactive backup operation on the '/home/user' directory
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p' # Copy directory content remotely via ssh
( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p' # Copy a local directory content remotely via ssh
tar cf - . | (cd /tmp/backup ; tar xf - ) # Locally copy a directory to another place, 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' to another directory
find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2 # Find all files ending with '.log' and create a bzip package
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 on a floppy disk
CD-ROM
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force # Clear the contents of a rewritable CD
mkisofs /dev/cdrom > cd.iso # Create an ISO image file of a CD on disk
mkisofs /dev/cdrom | gzip > cd_iso.gz # Create a compressed ISO image file of a CD on disk
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4 -o ./cd.iso data_cd # Create an ISO image file of a directory
cdrecord -v dev=/dev/cdrom cd.iso # Burn an ISO image file
gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom - # Burn a compressed ISO image file
mount -o loop cd.iso /mnt/iso # Mount an ISO image file
cd-paranoia -B # Transfer audio tracks from a CD to wav files
cd-paranoia -- "-3" # Transfer audio tracks from a CD to wav files (parameter -3)
cdrecord --scanbus # Scan the bus to identify the SCSI channel
dd if=/dev/hdc | md5sum # Check the md5sum encoding of a device, such as a CD
Network – (Ethernet and WIFI)
ifconfig eth0 # Display the configuration of an Ethernet card
ifup eth0 # Enable an 'eth0' network device
ifdown eth0 # Disable an 'eth0' network device
ifconfig eth0 192.168.1.1 netmask 255.255.255.0 # Control IP address
ifconfig eth0 promisc # Set 'eth0' to promiscuous mode to sniff packets
dhclient eth0 # Enable 'eth0' in DHCP mode
route -n # View the routing table
route add -net 0/0 gw IP_Gateway # Configure the default gateway
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 # Configure static routing to reach the network '192.168.0.0/16'
route del 0/0 gw IP_gateway # Delete static routing
hostname # View machine name
host www.example.com # Resolve a hostname to an internet address or resolve an internet address to a hostname.
nslookup www.example.com # Query DNS records, check if domain resolution is normal, used for diagnosing network issues during network failures.
ip link show # View network card information
mii-tool # Used to view and manage the status of media network interfaces
etstat -tupl # Display TCP/UDP status information
tcpdump tcp port 80 # Display all http protocol traffic
JPS Tool
jps (Java Virtual Machine Process Status Tool) is a command provided by JDK 1.5 that displays the current Java process PIDs, simple and practical, very suitable for quickly viewing the current Java process information on Linux/Unix platforms.
Many people have used the ps command in Unix systems, which is mainly used to display the current system’s process status, which processes are running, and their IDs. jps is similar, it shows the current Java process status and its ID. We can use it to check how many Java processes we have started (because each Java program will occupy a Java Virtual Machine instance), and their process numbers (to prepare for the following programs), and we can use the opt to view the detailed startup parameters of these processes.
Usage: Type jps in the current command line (requires JAVA_HOME, if not, go to the program directory to type).
jps is located in JAVA_HOME/bin/jps, for convenience, please add JAVA_HOME/bin to Path.
$> jps
23991 Jps
23789 BossMain
23651 Resin
Common parameters:
#-q Show only PID, do not show class name, jar file name and arguments passed to main method
$> jps -q
28680
23789
23651
#-m Output the arguments passed to the main method, may be null on embedded jvm
$> jps -m
28715 Jps -m
23789 BossMain
23651 Resin -socketwait 32768 -stdout /data/aoxj/resin/log/stdout.log -stderr /data/aoxj/resin/log/stderr.log
#-l Output the full package name of the application main class or the full path name of the application's jar file
$> jps -l
28729 sun.tools.jps.Jps
23789 com.asiainfo.aimc.bossbi.BossMain
23651 com.caucho.server.resin.Resin
#-v Output the parameters passed to JVM
$> jps -v
23789 BossMain
28802 Jps -Denv.class.path=/data/aoxj/bossbi/twsecurity/java/trustwork140.jar:/data/aoxj/bossbi/twsecurity/java/:/data/aoxj/bossbi/twsecurity/java/twcmcc.jar:/data/aoxj/jdk15/lib/rt.jar:/data/aoxj/jdk15/lib/tools.jar -Dapplication.home=/data/aoxj/jdk15 -Xms8m
jps 192.168.0.77
# List all jvm instances on the remote server 192.168.0.77, using rmi protocol, default connection port is 1099 (provided that the remote server provides jstatd service)
# Note: The jps command has a drawback, it seems that it can only display Java processes of the current user, to display other users' processes, you still have to use the unix/linux ps command.
– END –
///
Professional • Strength • Integrity • Value
WeChat ID:sannet-edu
Official Website:www.sannet.net
Consultation Hotline:0512-82289966
Address: Room 433, Heji Plaza, 666 Ganjiang East Road, Suzhou City