Table of contents
1. Character settings and file format conversion
Preface
This article continues to introduce some common Linux commands, including character settings and file format conversion, file system analysis, initializing a file system, backup, CD, and network. A complete collection of commonly used Linux commands and explanations (3) Please see the text for details.
For more common Linux commands and explanations, please see:
1. Character settings and file format conversion
Before executing these commands, make sure that the corresponding tools (such as dos2unix, unix2dos and recode) have been installed correctly, and back up your files in advance to prevent loss.
1.
dos2unix filedos.txt fileunix.txt
This command filedos.txt
converts an MSDOS format text file named to UNIX format and saves the result as fileunix.txt
.
2.
unix2dos fileunix.txt filedos.txt
This command fileunix.txt
converts a UNIX format text file named to MSDOS format and saves the result as filedos.txt
.
3.
recode ..HTML < page.txt > page.html
This command page.txt
converts a text file named to HTML format and outputs the results to page.html
a file named.
4.
recode -l | more
This command will display a list of all available character conversion formats in paginated format, and you can view them page by page.
2. File system analysis
1.
badblocks -v /dev/hda1
This command is used to check for bad blocks on disk "/dev/hda1" and displays the progress and results of the check in verbose mode.
2.
fsck /dev/hda1
This command is used to repair or check the integrity of the Linux file system on disk "/dev/hda1", and it will automatically perform necessary repair operations.
3.
fsck.ext2 /dev/hda1
or
e2fsck /dev/hda1
Both commands are used to repair or check the integrity of the ext2 file system on disk "/dev/hda1", you can choose to use one of them.
4.
e2fsck -j /dev/hda1
or
fsck.ext3 /dev/hda1
Both commands are used to repair or check the integrity of the ext3 file system on disk "/dev/hda1", you can choose to use one of them.
5.
fsck.vfat /dev/hda1
or
fsck.msdos /dev/hda1
or
dosfsck /dev/hda1
These commands are used to repair or check the integrity of the FAT file system or DOS file system on the disk "/dev/hda1". You can choose to use one of them.
3. Initialize a file system
1.
mkfs /dev/hda1
This command will create a file system on the hda1 partition. The specific file system type will depend on the system's default settings.
2.
mke2fs /dev/hda1
This command will create a Linux ext2 file system on the hda1 partition.
3.
mke2fs -j /dev/hda1
This command will create a Linux ext3 (with journaling) file system on the hda1 partition.
4.
mkfs -t vfat -F 32 /dev/hda1
This command will create a FAT32 file system on the hda1 partition.
5.
fdformat -n /dev/fd0
This command is used to format a floppy disk.
6.
mkswap /dev/hda3
This command will create a swap file system on the hda3 partition.
7.
swapon /dev/hda3
This command is used to enable a new swap file system.
8.
swapon /dev/hda2 /dev/hdb3
This command is used to enable multiple swap partitions, taking hda2 and hdb3 as examples.
4. Backup
1.
dump -0aj -f /tmp/home0.bak /home
This command will create a full backup of the '/home' directory and save it to the "/tmp/home0.bak" file.
2.
dump -1aj -f /tmp/home0.bak /home
This command will create an interactive backup of the '/home' directory and save it to the "/tmp/home0.bak" file.
3.
restore -if /tmp/home0.bak
This command will restore the contents of the backup from the "/tmp/home0.bak" file.
4.
rsync -rogpav --delete /home /tmp
This command will synchronize files and directories between the source directory "/home" and the target directory "/tmp", and ensure that the target directory is consistent with the source directory.
ip_address and ip_addr are placeholders in the example and need to be replaced with appropriate IP addresses or host names when used in practice .
5.
rsync -rogpav -e ssh --delete /home ip_address:/tmp
This command will use the SSH channel to synchronize the contents of the source directory "/home" to the target directory "/tmp" on the remote host.
6.
rsync -az -e ssh --delete ip_addr:/home/public /home/local
This command will locally synchronize the contents of the directory "/home/public" on the remote host to the local directory "/home/local" through the SSH channel, and use compression to speed up the transfer.
7.
rsync -az -e ssh --delete /home/local ip_addr:/home/public
This command will synchronize the contents of the directory "/home/local" locally to the directory "/home/public" on the remote host through an SSH channel, and use compression to speed up the transfer.
8.
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz'
This command will back up the contents of the local disk /dev/hda, transfer it to the remote host through the SSH channel, and save it as an hda.gz file.
9.
dd if=/dev/sda of=/tmp/file1
This command backs up the disk contents of /dev/sda to the file /tmp/file1.
10.
tar -Puf backup.tar /home/user
This command will perform an interactive backup operation on the directory /home/user and save the result as a backup.tar file.
11.
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p'
This command copies the contents of the local directory /tmp/local/ to the directory /home/share/ of the remote host through the SSH channel.
12.
( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p'
This command copies the contents of the local directory /home to the directory /home/backup-home of the remote host through the SSH channel.
13.
tar cf - . | (cd /tmp/backup ; tar xf - )
This command copies the contents of the current directory to another directory /tmp/backup, while retaining the original permissions and link relationships.
14.
find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents
This command will find all files ending with '.txt' in the directory /home/user1 and copy them to the directory /home/backup/, keeping the original directory structure.
15.
find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2
This command will find all files ending with '.log' in the directory /var/log and package them into a bzip2 compressed package log.tar.bz2.
16.
dd if=/dev/hda of=/dev/fd0 bs=512 count=1
This command will copy the MBR contents of disk /dev/hda to floppy disk /dev/fd0.
17.
dd if=/dev/fd0 of=/dev/hda bs=512 count=1
This command will restore the contents of the MBR copied from the floppy disk /dev/fd0 to the disk /dev/hda.
5. CD
1.
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force
This command will clear the contents of a rewritable disc and eject the disc.
2.
mkisofs /dev/cdrom > cd.iso
This command will create a CD ISO image file cd.iso from the optical drive device /dev/cdrom.
3.
mkisofs /dev/cdrom | gzip > cd_iso.gz
This command will create an ISO image file of a CD from the optical drive device /dev/cdrom and compress it into a cd_iso.gz file.
4.
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4 -o ./cd.iso data_cd
This command will create an ISO image file named cd.iso, which contains the contents of the directory data_cd and adds the label "Label CD".
5.
cdrecord -v dev=/dev/cdrom cd.iso
This command will burn the ISO image file named cd.iso to the optical drive device /dev/cdrom.
6.
gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom -
This command will burn a compressed ISO image file cd_iso.gz to the optical drive device /dev/cdrom.
7.
mount -o loop cd.iso /mnt/iso
This command mounts the ISO image file cd.iso to the directory /mnt/iso to access the contents.
8.
cd-paranoia -B
This command will rip audio tracks from a CD disc and save them as WAV files.
9.
cd-paranoia -- "-3"
This command will rip audio tracks from a CD disc and save them as WAV files. Parameter -3 specifies some specific options and can be modified as needed.
10.
cdrecord --scanbus
This command will scan the bus to identify SCSI channels and list available CD/DVD devices.
11.
dd if=/dev/hdc | md5sum
This command will read data from device /dev/hdc and calculate its MD5 checksum.
6. Network
1.
ifconfig eth0
This command will display the configuration information of the Ethernet card named eth0.
2.
ifup eth0
This command will enable the network device named eth0.
3.
ifdown eth0
This command will disable the network device named eth0.
4.
ifconfig eth0 192.168.1.1 netmask 255.255.255.0
This command will set the IP address of the network device named eth0 to 192.168.1.1 and the subnet mask to 255.255.255.0.
5.
ifconfig eth0 promisc
This command will set the network device named eth0 into promiscuous mode so that it can sniff network packets.
6.
dhclient eth0
This command will enable the network device named eth0 in DHCP mode, obtaining the IP address and configuration information from the DHCP server.
7.
route -n
This command will display the current system's routing table.
8.
route add -net 0/0 gw IP_Gateway
This command will configure the default gateway, where IP_Gateway is the IP address of the gateway to be set.
9.
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
This command will configure a static route to send traffic to the destination network '192.168.0.0/16', where IP address 192.168.1.1 is the gateway used to reach the destination network.
10.
route del 0/0 gw IP_gateway
This command will delete a static route, where IP_gateway is the gateway IP address of the route to be deleted.
11.
hostname
This command will display the machine name of the current host.
12.
host www.example.com
This command will resolve the hostname www.example.com to the corresponding IP address, or resolve the IP address to the hostname.
13.
nslookup www.example.com
This command is used to query DNS records and check whether domain name resolution is normal. Can be used to diagnose network problems when troubleshooting the network.
14.
ip link show
This command will display the network card information in the current system.
15.
mii-tool
This command is used to view and manage the status of the media network interface.
16.
ethtool
This command is used to query and set the configuration information of the network card.
17.
netstat -tupl
This command is used to display TCP/UDP status information on the current system.
18.
tcpdump tcp port 80
This command is used to capture and display all HTTP protocol traffic.
Summarize
This article is the last article summarizing common Linux commands. Learning Linux commands can improve system management and maintenance capabilities, expand technical breadth, and complete tasks more efficiently. At the same time, it cultivates problem solving and automated thinking and enhances technological competitiveness. Mastering Linux commands can also provide a deep understanding of the working principles of the operating system and network, laying a solid foundation for further learning and development.
Follow, like, collect, I hope friends can connect three times with one click!