Quick Reference for Common Linux commands
System information
arch | Display the processor architecture of the machine (1) |
---|---|
uname -m | Display the processor architecture of the machine (2) |
uname -r | Displays the kernel version in use |
dmidecode -q | Display hardware system components – (SMBIOS/DMI) |
hdparm -i /dev/hda | Lists the architectural features of a disk |
hdparm -tT /dev/sda | Perform a test read on disk |
cat /proc/cpuinfo | Display CPU info information |
cat /proc/interrupts | According to the interrupt |
cat /proc/meminfo | Checking memory usage |
cat /proc/swaps | Shows which swaps are used |
cat /proc/version | Displays the kernel version |
cat /proc/net/dev | Displays network adapters and statistics |
cat /proc/mounts | The loaded file system is displayed |
lspci -tv | List PCI devices |
lsusb -tv | Display USB device |
Date Displays the system date
cal 2007 | Displays the calendar for 2007 |
---|---|
date 20211207131313 | Set the date and time – month, day, hour, year. seconds |
clock -w | Save the time change to BIOS |
Shutdown (shutdown, reboot, and logout of the system)
shutdown -h now | Shutting down the System (1) |
---|---|
init 0 | Shutting down the System (2) |
telinit 0 | Shutting down the System (3) |
shutdown -h hours:minutes & | Shut down the system at a predetermined time |
shutdown -c | Cancel shutting down the system at the scheduled time |
shutdown -r now | Restart (1) |
reboot | Restart (2) |
logout | The cancellation |
Files and Directories
cd /home | Go to the ‘/ home’ directory |
---|---|
cd .. | Returns the upper directory |
cd .. /.. | Returns the previous two levels of directories |
cd | Go to your home directory |
cd ~user1 | Go to your home directory |
cd – | Returns the directory where you were last time |
pwd | Show working path |
ls | View the files in the directory |
ls -F | View the files in the directory |
ls -l | Displays file and directory details |
ls -a | Show hidden files |
ls *[0-9]* | Displays file names and directory names that contain numbers |
tree | Displays the tree structure of files and directories starting from the root directory (1) |
lstree | Displays the tree structure of files and directories starting from the root directory (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 delete its contents simultaneously |
rm -rf dir1 dir2 | Delete both directories and their contents simultaneously |
mv dir1 new_dir | Rename/move a directory |
cp file1 file2 | Copy a file |
cp dir/* . | Copy all files in 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 soft link to a file or directory |
ln file1 lnk1 | Create a physical link to a file or directory |
touch -t 0712250000 file1 | Modify the timestamp of a file or directory – (YYMMDDhhmm) |
iconv -l | List the known codes |
File search
find / -name file1 | Start with ‘/’ and go to the root file system to search for files and directories |
---|---|
find / -user user1 | Search for files and directories belonging to user ‘user1’ |
find /home/user1 -name *.bin | Search for files ending in’.bin’ in the directory ‘/home/user1’ |
find /usr/bin -type f -atime +100 | Searches for executable files that have not been used in the past 100 days |
find /usr/bin -type f -mtime -10 | Search for files that have been created or modified in the last 10 days |
find / -name *.rpm -exec chmod 755 ‘{}’ ; | Search for files ending in ‘.rpm’ and define their permissions |
find / -xdev -name *.rpm | RPM files ending in ‘.rpm’ are searched, ignoring removable devices such as cd-rom drives and flash drives |
locate *.ps | Find files ending in ‘.ps’ – run the ‘updatedb’ command first |
whereis halt | Displays the location of a binary, source, or MAN |
which halt | Displays the full path to a binary or executable file |
Mount a file system
df -h | Displays a list of mounted partitions |
---|---|
ls -lSr | more |
du -sh dir1 | Estimate the disk space already used by directory ‘dir1’ |
Users and Groups
groupadd group_name | Create a new user group |
---|---|
groupdel group_name | Example Delete a user group |
groupmod -n new_group_name old_group_name | Renames 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 | Modifying User Attributes |
passwd | Change password |
passwd user1 | Change a user’s password (root only) |
chage -E 2005-12-31 user1 | Set the validity period of the user password |
pwck | Check for file format and syntax fixes for ‘/etc/passwd’ and for existing users |
grpck | Check for file format and syntax fixes for ‘/etc/passwd’ and existing groups |
newgrp group_name | Log in to a new group to change the default group for newly created files |
File permissions
ls -lh | Show permissions |
---|---|
ls /tmp | pr -T5 -W$COLUMNS |
chmod ugo+rwx directory1 | Set owner (u), group (g), and other (O) permissions to read (r), write (w), and execute (x) of the directory |
chmod go-rwx directory1 | Delete the read/write permissions of group (g) and others (O) on directories |
chown user1 file1 | Change the owner attribute of a file |
chown -R user1 directory1 | Change the owner properties of a directory and change the properties of all files in the directory |
chgrp group1 file1 | Change the group of files |
chown user1:group1 file1 | Change the owner and group properties of a file |
find / -perm -u+s | List all files in a system that use SUID control |
chmod u+s /bin/file1 | Set the SUID bit of a binary file – the user running the file is also given the same permissions as the owner |
chmod u-s /bin/file1 | Disables the SUID bit of a binary file |
chmod g+s /home/public | Sets the SGID bit of a directory – similar to SUID, but specific to the directory |
chmod g-s /home/public | To disable the SGID bit of a directory |
chmod o+t /home/public | Set the STIKY bits of a file – only allow the legal owner to delete the file |
chmod o-t /home/public | Disable the STIKY bit for a directory |
Special properties of the file
chattr +a file1 | Only files can be read or written appending |
---|---|
chattr +c file1 | Allows this file to be automatically compressed/uncompressed by the kernel |
chattr +d file1 | This file is ignored by the dump program during a file system backup |
chattr +i file1 | Set to an immutable file that cannot be deleted, modified, renamed, or linked |
chattr +s file1 | Allows a file to be safely deleted |
chattr +S file1 | Once the application writes to the file, cause the system to write the changes to disk immediately |
chattr +u file1 | If the file is deleted, the system will allow you to restore the deleted file at a later time |
lsattr | Display special properties |
Package and compress files
bunzip2 file1.bz2 | Unzip a file called ‘file1.bz2’ |
---|---|
bzip2 file1 | Zip a file called ‘file1’ |
gunzip file1.gz | Unzip a file called ‘file1.gz’ |
gzip file1 | Zip a file called ‘file1’ |
gzip -9 file1 | Maximum compression |
rar a file1.rar test_file | Create a package called ‘file1.rar’ |
rar a file1.rar file1 file2 dir1 | Compress ‘file1′,’file2’ and directory ‘dir1’ simultaneously |
rar x file1.rar | Decompress rar package |
unrar x file1.rar | Decompress rar package |
tar -cvf archive.tar file1 | Create an uncompressed tarball |
tar -cvf archive.tar file1 file2 dir1 | Create a file containing ‘file1′,’file2’, and ‘dir1’ |
tar -tf archive.tar | Displays the contents of a package |
tar -xvf archive.tar | Release a package |
tar -xvf archive.tar -C /tmp | Release the compressed package to the/TMP directory |
tar -cvfj archive.tar.bz2 dir1 | Create a zip package in bzip2 format |
tar -jxvf archive.tar.bz2 | Unzip a bzip2 package |
tar -cvfz archive.tar.gz dir1 | Create a gzip zip package |
tar -zxvf archive.tar.gz | Decompress a gzip package |
zip file1.zip file1 | Create a zip package in zip format |
zip -r file1.zip file1 file2 dir1 | Compress several files and directories into a ZIP package |
unzip file1.zip | Decompress a zip package |
RPM package – (Fedora, Redhat, and similar systems)
rpm -ivh package.rpm | Install an RPM package |
---|---|
rpm -ivh –nodeeps package.rpm | Install an RPM package and ignore dependency warnings |
rpm -U package.rpm | Update an RPM package without changing its configuration file |
rpm -F package.rpm | Update an RPM package that is confirmed to be installed |
rpm -e package_name.rpm | Example Delete an RPM package |
rpm -qa | All RPM packages installed in the system are displayed |
rpm -qa | grep httpd |
rpm -qi package_name | Gets special information about an installed package |
rpm -qg “System Environment/Daemons” | Displays an RPM package for a component |
rpm -ql package_name | Displays a list of files provided by an installed RPM package |
rpm -qc package_name | Displays a list of configuration files provided by an installed RPM package |
rpm -q package_name –whatrequires | Displays a list of dependencies that exist with an RPM package |
rpm -q package_name –whatprovides | Display the volume occupied by an RPM package |
rpm -q package_name –scripts | Displays the script L executed during installation/deletion |
rpm -q package_name –changelog | Display the change history of an RPM package |
rpm -qf /etc/httpd/conf/httpd.conf | Verify which RPM package provides the given file |
rpm -qp package.rpm -l | Displays a list of files provided by an RPM package that has not yet been installed |
rpm –import /media/cdrom/RPM-GPG-KEY | Import a public key digital certificate |
rpm –checksig package.rpm | Verify the integrity of an RPM package |
rpm -qa gpg-pubkey | Verify the integrity of all RPM packages installed |
rpm -V package_name | Check file size, license, type, owner, group, MD5 check, and last modified time |
rpm -Va | Check all installed RPM packages on your system – use with care |
rpm -Vp package.rpm | Verify that an RPM package is not installed |
rpm2cpio package.rpm | cpio –extract –make-directories *bin* |
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 Upgrader – (Fedora, RedHat and similar systems)
yum install package_name | Download and install an RPM package |
---|---|
yum localinstall package_name.rpm | An RPM package will be installed and use your own software repository to resolve all dependencies for you |
yum update package_name.rpm | Update all RPM packages installed in the current system |
yum update package_name | Update an RPM package |
yum remove package_name | Example Delete an RPM package |
yum list | Lists all packages installed on the current system |
yum search package_name | Search the RPM repository for packages |
yum clean packages | Clear the RPM cache and delete the 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 | Delete a DEB package from the system |
dpkg -l | Displays all deb packages installed on your system |
dpkg -l | grep httpd |
dpkg -s package_name | Gets information about a special package that has been installed on the system |
dpkg -L package_name | Displays a list of files provided by a DEB package that has been installed on your system |
dpkg –contents package.deb | Displays a list of files provided by a package that has not yet been installed |
dpkg -S /bin/ping | Verify 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 software packages in the list |
apt-get upgrade | Upgrade all installed software |
apt-get remove package_name | Delete a DEB package from the system |
apt-get check | Verify that the software repository you depend on is correct |
apt-get clean | Clear the cache from downloaded packages |
apt-cache search searched-package | Returns the name of the package containing the string to search for |
Viewing file Contents
cat file1 | Forward view the contents of the file from the first byte |
---|---|
tac file1 | View the contents of a file in reverse, starting with the last line |
more file1 | View the contents of a long file |
less file1 | Similar to the ‘more’ command, but it allows the same reverse operation in the file as the forward operation |
head -2 file1 | Look at the first two lines of a file |
tail -2 file1 | Look at the last two lines of a file |
tail -f /var/log/messages | See what is added to a file in real time |
Text processing
cat file1 | command( sed, grep, awk, grep, etc…) > result.txt | Merges the detailed text of a file and writes the introduction to a new file |
---|---|
cat file1 | command( sed, grep, awk, grep, etc…) >> result.txt | Merges the detailed text of a file and writes the introduction to an existing file |
grep Aug /var/log/messages | Find the key “Aug” in the file ‘/var/log/messages’ |
grep ^Aug /var/log/messages | Look 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 example.txt |
sed ‘/^$/d’ example.txt | Remove all blank lines from example.txt |
sed ‘/ *#/d; /^$/d’ example.txt | Remove all comments and blank lines from example.txt |
echo ‘esempio’ | tr ‘[:lower:]’ ‘[:upper:]’ | Merges the contents of upper and lower cells |
sed -e ‘1d’ result.txt | Exclude the first line from the file example.txt |
sed -n ‘/stringa1/p’ | View lines that contain only the word “string1” |
sed -e ‘s/ *$//’ example.txt | Remove the whitespace character at the end of each line |
sed -e ‘s/stringa1//g’ example.txt | Remove only the word “string1” from the document and keep the rest |
Sed – n ‘1, 5 p; 5q’ example.txt | Look at lines 1 through 5 |
sed -n ‘5p; 5q’ example.txt | Look at the first row |
sed -e ‘s/00*/0/g’ example.txt | Replace multiple zeros with a single zero |
cat -n file1 | Indicates the number of files |
cat example.txt | awk ‘NR%2==1’ | Delete all even lines in example.txt |
echo a b c | awk ‘{print $1}’ | Look at the first column of a row |
echo a b c | awk ‘{print 3} ‘ |
Look at the first and third columns of a row |
paste file1 file2 | Combine the contents of two files or columns |
paste -d ‘+’ file1 file2 | Combine the contents of two files or columns separated by a “+” |
sort file1 file2 | Sort the capacity of two files |
sort file1 file2 | uniq | Retrieve the union of two files (only one duplicate line is kept) |
sort file1 file2 | uniq -u | Delete the intersection and leave the other rows |
sort file1 file2 | uniq -d | Extract the intersection of two files (leaving only files that exist in both files) |
comm -1 file1 file2 | Compare the contents of two files and remove only those contained in ‘file1’ |
comm -2 file1 file2 | Compare the contents of two files and remove only those contained in ‘file2’ |
comm -3 file1 file2 | Compare the contents of two files and delete only the parts that are common to the two 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 |
File System Analysis
badblocks -v /dev/hda1 | Check the bad magnetic block on disk HDA1 |
---|---|
fsck /dev/hda1 | Fix/check the integrity of the Linux file system on hDA1 disk |
fsck.ext2 /dev/hda1 | Repair/check the integrity of the ext2 file system on hda1 disk |
e2fsck /dev/hda1 | Repair/check the integrity of the ext2 file system on hda1 disk |
e2fsck -j /dev/hda1 | Repair/check the integrity of the ext3 file system on hda1 disk |
fsck.ext3 /dev/hda1 | Repair/check the integrity of the ext3 file system on hda1 disk |
fsck.vfat /dev/hda1 | Fix/check the integrity of the FAT file system on hda1 disk |
fsck.msdos /dev/hda1 | Fix/check the integrity of the DOS file system on hDA1 disk |
dosfsck /dev/hda1 | Fix/check the integrity of the DOS file system on hDA1 disk |
Example Initialize a file system
mkfs /dev/hda1 | Create a file system in the HDA1 partition |
---|---|
mke2fs /dev/hda1 | Create a Linux ext2 filesystem in the hda1 partition |
mke2fs -j /dev/hda1 | Create a Linux ext3(journaling) file system in the Hda1 partition |
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 | Start a new swap file system |
swapon /dev/hda2 /dev/hdb3 | Enable two swap partitions |
The 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 directories on both sides |
rsync -rogpav -e ssh –delete /home ip_address:/tmp | Rsync through SSH channel |
rsync -az -e ssh –delete ip_addr:/home/public /home/local | Synchronize a remote directory to a local directory using SSH and compression |
rsync -az -e ssh –delete /home/local ip_addr:/home/public | Synchronize local directories to remote directories using SSH and compression |
dd bs=1M if=/dev/hda | gzip |
dd if=/dev/sda of=/tmp/file1 | Backup disk contents to a file |
Tar -puf backup.tar /home/user Perform a single call to ‘/home/user’ | Interactive backup operations for directories |
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr ‘cd /home/share/ && tar x -p’ |
( tar c /home ) | ssh -C user@ip_addr ‘cd /home/backup-home && tar x -p’ |
tar cf – . | (cd /tmp/backup ; tar xf – ) |
find /home/user1 -name ‘*.txt’ | xargs cp -av –target-directory=/home/backup/ –parents |
find /var/log -name ‘*.log’ | tar cv –files-from=- |
dd if=/dev/hda of=/dev/fd0 bs=512 count=1 | Make an action to copy the contents of the MBR (Master Boot Record) to the floppy disk |
dd if=/dev/fd0 of=/dev/hda bs=512 count=1 | Restores MBR content from a backup already saved to floppy disk |
CD
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force | Clear the contents of a reproducible CD |
---|---|
mkisofs /dev/cdrom > cd.iso | Create an ISO image file on the disk |
mkisofs /dev/cdrom | gzip > cd_iso.gz |
mkisofs -J -allow-leading-dots -R -V “Label CD” -iso-level 4 -o ./cd.iso data_cd | Create an ISO image file for a directory |
cdrecord -v dev=/dev/cdrom cd.iso | Burn an ISO image file |
gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom – |
mount -o loop cd.iso /mnt/iso | Example Mount an ISO image file |
cd-paranoia -B | Transcribe audio tracks from a CD to a WAV file |
cd-paranoia — “-3” | Transcribe an audio track from a CD to a WAV file (parameter -3) |
cdrecord –scanbus | Scan the bus to identify SCSI channels |
dd if=/dev/hdc | md5sum |
Networking – (Ethernet and WIFI wireless)
ifconfig eth0 | Displays 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 mingle mode to sniff packets |
dhclient eth0 | Enable ‘eth0’ in DHCP mode |
route -n | Viewing the Routing Table |
route add -net 0/0 gw IP_Gateway | Configuring the Default Gateway |
Route add-net 192.168.0.0 netmask 255.255.0.0 GW 192.168.1.1 | Configure static route to network ‘192.168.0.0/16’ |
route del 0/0 gw IP_gateway | Deleting a Static Route |
hostname | View machine name |
host www.example.com | Resolves a host name to an Internet address or an Internet address to a host name. |
nslookup www.example.com | It is used to query DNS records to check whether domain names are resolved properly and diagnose network faults. |
ip link show | Viewing NIC Information |
mii-tool | Used to view and manage the status of network interfaces on media |
ethtool | This command is used to query and set NIC configurations |
netstat -tupl | This command is used to display TCP/UDP status information |
tcpdump tcp port 80 | Displays all HTTP traffic |
If you need the PDF document, please scan the qr code and pay attention to the following qr code
Rumenz.com/examples/li…