I've been dealing with Linux lately and it's feeling pretty good. I think the more troublesome thing about Linux compared to windows is that many things have to be controlled by commands, of course, this is also the reason why many people like Linux, which is relatively short but powerful. I will list the commands I have learned for your reference only:
System information Processor architecture of arch display machines(1) uname -m shows the processor architecture of the machine (2) uname -r shows the kernel version being used dmidecode -q Display Hardware System Parts - (SMBIOS / DMI) hdparm -i /dev/hda lists the architectural characteristics of a disk hdparm -tT /dev/sda performs a test read operation on disk cat /proc/cpuinfo displays the information of the CPU info cat /proc/interrupts to show the interrupt cat /proc/meminfo to check memory usage cat /proc/swaps shows which swaps are used cat /proc/version shows the version of the kernel cat /proc/net/dev displays network adapters and statistics cat /proc/mounts shows the loaded file system lspci -tv lists PCI devices lsusb -tv shows USB devices date displays the system date Cal 2007 shows the calendar table for 2007 Date 041217002007.00 Set the date and time - month, day, time, year. seconds clock -w to save time modifications to the BIOS
Shutdown (shutdown, restart, and logout of the system) shutdown -h now shutdown system(1) init 0 shut down the system (2) telinit 0 shutdown system(3) shutdown -h hours:minutes & shut down the system at a scheduled time shutdown -c cancel shutdown of the system at a scheduled time shutdown -r now restart(1) Reboot (2) logout
Files and directories cd /home into the '/home' directory cd .. Returns to the previous directory cd .. /.. Return to the previous two levels of directory CD into the individual's home directory cd ~user1 to enter the individual's home directory cd - Returns to the directory where it was last located pwd shows the working path ls to view the files in the directory ls -F to view the files in the directory ls -l displays the details of the file and directory ls -a shows hidden files ls *[0-9]* displays the file name and directory name that contain the number 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 creates two directories at the same time mkdir -p /tmp/dir1/dir2 to create a directory tree rm -f file1 deletes a file called 'file1' rmdir dir1 deletes a directory called 'dir1'' rm -rf dir1 deletes a directory called 'dir1' and deletes its contents at the same time rm -rf dir1 dir2 deletes both directories and their contents at the same time mv dir1 new_dir rename/move a directory cp file1 file2 copy a file cp dir/* . Copy all files in one directory to the current working directory cp -a /tmp/dir1 . Copy a directory to the current working directory cp -a dir1 dir2 duplicate a directory ln -s file1 lnk1 creates a softlink to a file or directory ln file1 lnk1 creates a physical link 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 lists 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 goes to the root file system from '/' to search for files and directories find / -user user1 searches for files and directories belonging to user 'user1' find /home/user1 -name \*.bin Search for files with the ending '.bin' in the directory '/home/user1' find /usr/bin -type f -atime +100 Search for executables that have not been used in the last 100 days find /usr/bin -type f -mtime -10 Search for files that have been created or modified within 10 days find / -name \*.rpm -exec chmod 755 '{}' \; Search for files ending in '.rpm' and define their permissions find / -xdev -name \*.rpm searches for files ending in '.rpm', ignoring removable devices such as optical drives and shortcuts locate \*.ps to look for files ending in '.ps' - run the 'updatedb' command first Whereis halt shows the location of a binary, source code, or man which halt shows the full path to a binary or executable
Mount a file system mount /dev/hda2 /mnt/hda2 mount a disk called hda2 - make sure the directory '/mnt/hda2' already exists umount /dev/hda2 uninstall a disk called hda2 - exit from mount point '/mnt/hda2' first fuser -km /mnt/hda2 force uninstall when the device is busy umount -n /mnt/hda2 runs an uninstall operation without writing to the /etc/mtab file - useful when the file is read-only or when the disk is full of writes mount /dev/fd0 /mnt/floppy to mount a floppy disk mount /dev/cdrom /mnt/cdrom mounts a cdrom or dvdrom mount /dev/hdc /mnt/cdrecorder to mount a cdrw or dvdrom mount /dev/hdb /mnt/cdrecorder mounts a cdrw or dvdrom mount -o loop file.iso /mnt/cdrom to mount a file or ISO image file mount -t vfat /dev/hda5 /mnt/hda5 mounts a Windows FAT32 file system mount /dev/sda1 /mnt/usbdisk Mount a USB shortcut or flash device mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share Mount a windows network share
Disk space df -h shows a list of partitions that have been mounted ls -lSr |more arranges files and directories by size du -sh dir1 estimates directory 'dir1' disk space already used' du -sk * | sort -rn displays the size of the file and the directory in order of capacity rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n Displays the space used by installed rpm packages in order of size (fedora, redhat class system) dpkg-query -W -f='${Installed-Size; 10}t${Package}n' | sort -k1,1n Shows the space used by installed deb packages based on size (Ubuntu, Debian class systems)
Users and groups groupadd group_name to 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 that belongs to the "admin" user group useradd user1 to create a new user userdel -r user1 deletes a user ('-r' excludes the home directory) usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 Modify user attributes passwd modifies the password passwd user1 modifies a user's password (only root is allowed to execute) chage -E 2005-12-31 user1 Set the expiration period of the user password pwck checks for file formatting and syntax corrections for '/etc/passwd' and the users present grpck checks for file format and syntax corrections for '/etc/passwd' and the groups present newgrp group_name logs into a new group to change the default group for the newly created file
Permissions for files - Use "+" to set permissions, use "-" for cancel ls -lh to show permissions ls /tmp | pr -T5 -W$COLUMNS divides the terminal into 5 columns chmod ugo+rwx directory1 sets the permissions of everyone (u), group (g), and others (o) to read (r), write (w), and execute (x) of the directory chmod go-rwx directory1 removes the read and write execution permissions of the group (g) and other people (o) on the directory chown user1 file1 changes the owner property of a file chown -R user1 directory1 changes the properties of all files in a directory and changes the properties of all files in the directory at the same time chgrp group1 file1 changes the group of files chown user1:group1 file1 changes the owner and group properties of a file find / -perm -u+s lists all files in a system that use SUID control chmod u+s /bin/file1 sets the SUID bit of the binary file - the user running the file is given the same permissions as the owner chmod u-s /bin/file1 disables the SUID bit of a binary chmod g+s /home/public sets the SGID bit of a directory - similar to SUID, but this is directory-specific chmod g-s /home/public disables the SGID bit of a directory chmod o+t /home/public sets the STIKY bit of a file - only the rightful owner is allowed to delete the file chmod o-t /home/public disables the STIKY bits of a directory
Special properties of the file - set permissions with "+" and "-" for cancel Chattr +A file1 only allows you to read and write files in an append-like manner Chattr +C file1 allows this file to be automatically compressed/decompressed by the kernel chattr +d file1 The dump program will ignore this file when performing a file system backup Chattr +i file1 is set to be an immutable file and cannot be deleted, modified, renamed, or linked Chattr +S file1 allows a file to be securely deleted chattr +S file1 Once the application performs a write operation on this file, the system will immediately write the result of the change to the disk chattr +u file1 If the file is deleted, you will be allowed to recover the deleted file later lsattr displays special properties
Package and compress files bunzip2 file1.bz2 unzips a file called file1.bz2 bzip2 file1 compresses a file called 'file1' gunzip file1.gz unzip a file called 'file1.gz' gzip file1 compresses 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 compresses 'file1', 'file2' and directory 'dir1' at the same time RAR X file1.rar Unzip RAR packages Unrar X file1.rar Unzip RAR package tar -cvf archive.tar file1 to create an uncompressed tarball tar -cvf archive.tar file1 file2 dir1 creates an archive 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 releases the compressed package to the /tmp directory tar -cvfj archive.tar.bz2 dir1 creates a bzip2 formatted compressed package tar -jxvf archive.tar.bz2 to unzip a bzip2 format compressed package tar -cvfz archive.tar.gz dir1 to create a gzip package tar -zxvf archive.tar.gz Unzip a gzip package zip file1.zip file1 to create a zip package zip -r file1.zip file1 file2 dir1 compresses several files and directories into a zip package at the same time unzip file1.zip Unzip a zip format compressed package
RPM Packages - (Fedora, Redhat and similar) rpm -ivh package.rpm Install an rpm package rpm -ivh --nodeeps package.rpm installs an rpm package and ignores dependency warnings rpm -U package.rpm Updates an rpm package without changing its configuration file rpm -F package.rpm Update an rpm package that is sure to have been installed rpm -e package_name.rpm deletes an rpm package rpm -QA displays all RPM packages that have been installed in the system rpm -qa | grep httpd shows all rpm packages with the word "httpd" in their names rpm -qi package_name to get special information for an installed package rpm -qg "System Environment/Daemons" displays the rpm package for a component rpm -ql package_name displays a list of files provided by an already installed rpm package rpm -qc package_name displays a list of configuration files provided by an already installed rpm package rpm -q package_name --whatrequires shows a list of dependencies with an rpm package rpm -q package_name --whatprovides shows the volume occupied by an rpm packet rpm -q package_name --scripts shows the scripts executed during installation/deletion rpm -q package_name --changelog shows the modification history of an rpm package rpm -qf /etc/httpd/conf/httpd.conf to confirm which rpm package the file is provided by 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 to import the public key digital certificate rpm --checksig package.rpm confirms the integrity of an rpm package rpm -qa gpg-pubkey confirms the integrity of all installed rpm packages rpm -V package_name check file size, license, type, owner, group, MD5 check, and last modified time rpm -VA checks all installed rpm packages in the system - Use with care rpm -Vp package.rpm confirms that an rpm package is not yet installed rpm2cpio package.rpm | cpio --extract --make-directories *bin* runs the executable from an rpm package rpm -ivh /usr/src/redhat/RPMS/'arch'/package.rpm Install a built package from an rpm source code rpmbuild --rebuild package_name.src.rpm Build an rpm package from one rpm source code
YUM Package Upgrader - (Fedora, RedHat and similar) yum install package_name Download and install an rpm package yum localinstall package_name.rpm will install an rpm package that uses your own repository to resolve all dependencies for you yum update package_name.rpm updates all installed rpm packages in the current system yum update package_name update an rpm package yum remove package_name deletes an rpm packet The yum list lists all the packages installed in the current system yum search package_name searches for packages in the rpm repository yum clean packages cleans rpm cache and deletes downloaded packages yum clean headers deletes all header files yum clean all deletes all cached packages and header files
DEB packages (Debian, Ubuntu and similar) dpkg -i package.deb install/update a deb package dpkg -r package_name remove a deb package from the system dpkg -l shows all the deb packages that have been installed in the system dpkg -l | grep httpd displays all deb packages with the word "httpd" in their names dpkg -s package_name get information about a special package that is already installed in the system dpkg -L package_name displays a list of files provided by a deb package that has been installed in the system dpkg --contents package.deb displays a list of files provided by a package that has not yet been installed dpkg -S /bin/ping to confirm which deb package is provided by the given file
APT software tools (Debian, Ubuntu and similar) apt-get install package_name install/update a deb package apt-cdrom install package_name install/update a deb package from the disc apt-get update package in the upgrade list apt-get upgrade Upgrade all installed software apt-get remove package_name Delete a deb package from the system apt-get check confirms that the dependent repository is correct apt-get clean cleans the cache from the downloaded package apt-cache search searched-package returns the package name that contains the string you want to search
View the contents of the file cat file1 looks forward at the contents of the file from the first byte tac file1 looks at the contents of a file in reverse from the last line more file1 to view the contents of a long file less file1 is similar to the 'more' command, but it allows the same reverse operation in the file as it does in the forward operation head -2 file1 looks at the first two lines of a file tail -2 file1 to see the last two lines of a file tail -f /var/log/messages to see what has been added to a file in real time
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 the detailed description text of a file and write the introduction into a new file cat file1 | command( sed, grep, awk, grep, etc...) >> result.txt Merge the detailed description text of a file and write the introduction into an existing file grep Aug /var/log/messages Find the keyword "Aug" in the file '/var/log/messages' grep ^Aug /var/log/messages Look for words that start with "Aug" in the file '/var/log/messages' grep [0-9] /var/log/messages Select all the lines that contain 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 replaces "string1" with "string2" in the example.txt file sed '/^$/d' example.txt Remove all blank lines from the example.txt file sed '/ *#/d; /^$/d' example.txt Deletes all comments and blank lines from the example.txt file echo 'esempio' | tr '[:lower:]' '[:upper:]' merges the upper and lower cell contents sed -e '1d' result.txt exclude the first line from the file example.txt sed -n '/stringa1/p' to see lines that contain only the word "string1" sed -e 's/ *$//' example.txt Remove the last whitespace character of 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 the content from line 1 to line 5 sed -n '5p; 5q' example.txt Look at line 5 sed -e 's/00*/0/g' example.txt replaces multiple zeros with a single zero cat -n file1 indicates the number of lines in the file cat example.txt | awk 'NR%2==1' deletes all even lines in example.txt file echo a b c | awk '{print $1}' to look at the first column of a line echo a b c | awk '{print $1,$3}' to look at the first and third columns of a row paste file1 file2 merges the contents of two files or columns paste -d '+' file1 file2 merges the contents of two files or columns, separated by a "+" in the middle sort file1 file2 sorts the contents of both files sort file1 file2 | uniq takes out the union of two files (only one copy of duplicate lines is kept) sort file1 file2 | uniq -u deletes the intersection, leaving the other lines sort file1 file2 | uniq -d takes out the intersection of two files (leaving only files that exist in both files at the same time) comm -1 file1 file2 compares the contents of the two files and only deletes what 'file1' contains comm -2 file1 and file2 compare the contents of the two files and delete only the contents contained in 'file2' comm -3 file1 file2 compares the contents of the two files and only deletes the parts common to both files
Character settings and file format conversion dos2unix filedos.txt fileunix.txt converts the format of a text file from MSDOS to UNIX unix2dos fileunix.txt filedos.txt converts the format of a text file from UNIX to MSDOS recode .. HTML < page.txt > page.html Converts a text file into html recode -l | more shows all allowed conversion formats
File system analysis badblocks -v /dev/hda1 to check for bad blocks on disk hda1 fsck /dev/hda1 to repair/check the integrity of the Linux file system on the hda1 disk fsck.ext2 /dev/hda1 Repair/check the integrity of the ext2 file system on the hda1 disk e2fsck /dev/hda1 Repair/check the integrity of the ext2 file system on the hda1 disk e2fsck -j /dev/hda1 repairs/checks the integrity of the ext3 file system on the hda1 disk fsck.ext3 /dev/hda1 repairs/checks the integrity of the ext3 file system on the hda1 disk fsck.vfat /dev/hda1 repairs/checks the integrity of the FAT file system on the hda1 disk fsck.msdos /dev/hda1 to repair/check the integrity of the DOS file system on the hda1 disk dosfsck /dev/hda1 repairs/checks the integrity of the DOS file system on the hda1 disk
Initialize a file system mkfs /dev/hda1 creates a file system in the hda1 partition mke2fs /dev/hda1 creates a Linux ext2 file system in the hda1 partition mke2fs -j /dev/hda1 creates a linux ext3 (log-based) filesystem in the hda1 partition mkfs -t vfat 32 -F /dev/hda1 to create a FAT32 file system fdformat -n /dev/fd0 formats a floppy disk mkswap /dev/hda3 to create a swap filesystem
SWAP file system mkswap /dev/hda3 to create a swap filesystem swapon /dev/hda3 enables a new swap file system swapon /dev/hda2 /dev/hdb3 enables both swap partitions
backup dump -0aj -f /tmp/home0.bak /home to make a full backup of the '/home' directory dump -1aj -f /tmp/home0.bak /home to make an interactive backup of the '/home' directory restore -if /tmp/home0.bak to restore an interactive backup rsync -rogpav --delete /home /tmp to synchronize the directories on both sides rsync -rogpav -e ssh --delete /home ip_address:/tmp rsync via SSH channel rsync -az -e ssh --delete ip_addr:/home/public /home/local syncs a remote directory to the local directory via ssh and compression rsync -az -e ssh --delete /home/local ip_addr:/home/public syncs the local directory to the remote directory via ssh and compression dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' performs a one-time backup of the local disk on the remote host via ssh dd if=/dev/sda of=/tmp/file1 back up disk contents to a file tar -Puf backup.tar /home/user perform an interactive backup operation to the '/home/user' directory once ( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ &&> tar x -p' Copy one directory content in a remote directory via ssh ( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p' Copy a local directory in a remote directory via ssh tar cf - . | (cd /tmp/backup ; tar xf - ) locally copies one directory to another, retaining the original permissions and links find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents finds and copies all files ending in '.txt' from one directory to another find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2 finds all files ending in '.log' and makes a bzip package dd if=/dev/hda of=/dev/fd0 bs=512 count=1 Do an action to copy the MBR (Master Boot Record) content to the floppy disk dd if=/dev/fd0 of=/dev/hda bs=512 count=1 Recover MBR content from a backup that has been saved to the floppy disk
CD cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force Empty the contents of a copyable disc mkisofs /dev/cdrom > cd.iso Create an ISO image file of the disc on disk mkisofs /dev/cdrom | gzip > cd_iso.gz creates a compressed disc ISO image file 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 the directory cdrecord -v dev=/dev/cdrom cd.iso Burn an ISO image file gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom - burns a compressed ISO image file mount -o loop cd.iso /mnt/iso to mount an ISO image file cd-paranoia -B Transcribed the track from a CD disc into a wav file cd-paranoia -- "-3" transcribed audio tracks from a CD disc into a wav file (parameter-3) cdrecord --scanbus scans the bus to identify the scsi channel dd if=/dev/hdc | md5sum validates the md5sum encoding of a device, such as a CD
Network - (Ethernet & WIFI wireless) ifconfig eth0 shows the configuration of an Ethernet card ifup eth0 enables an 'eth0' network device ifdown eth0 disables an 'eth0' network device ifconfig eth0 192.168.1.1 netmask 255.255.255.0 control IP address ifconfig eth0 promisc sets 'eth0' to promiscuous mode to sniff packets (sniffing) dhclient eth0 enables '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 hostwww.example.comlookup hostname to resolve name to ip address and viceversa(1) nslookupwww.example.comlookup 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 hostwww.example.comlookup hostname to resolve name to ip address and viceversa nslookupwww.example.comlookup hostname to resolve name to ip address and viceversa whoiswww.example.comlookup on Whois database JPS tool jps (Java Virtual Machine Process Status Tool) is a command provided by JDK 1.5 that displays the pids of all current java processes, which is simple and practical, and is very suitable for simply checking some simple situations of the current java process on the Linux/Unix platform. I think many people have used the ps command in the UNIX system, which is mainly used to display the current system processes, which processes are there, and their IDs. The same goes for jps, which shows the current system's Java process status and its ID number. It allows us to see how many Java processes we have started (because each Java program has an exclusive Java VM instance), their process numbers (in preparation for the following programs), and we can use opt to see the detailed startup parameters of these processes. How to use: Type jps under the current command line (need JAVA_HOME, if not, go to the directory of the change program). jps are stored in JAVA_HOME/bin/jps, so please add JAVA_HOME/bin/ to Path.$> jps for convenience 23991 Jps 23789 BossMain 23651 Resin The most commonly used parameters: -q only displays the pid, not the class name, jar file name, and the parameters passed to the main method $> jps -q
28680
23789 23651-m outputs the parameters passed to the main method, which may be null$> jps -m on the embedded JVM 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's 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 outputs the parameter $> jps -v passed to the JVM 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 23651 Resin -Xss1m -Dresin.home=/data/aoxj/resin -Dserver.root=/data/aoxj/resin -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial= com.caucho.jmx.MBeanServerBuilderImplsudo jps sees the most complete number of processes 192.168.0.77 lists all JVM instances of the remote server 192.168.0.77 machine, using RMI protocol, the default connection port is 1099 (provided that the remote server provides jstatd service) Note: There is a bad place in the jps command, it seems that it can only display the java process of the current user, and you can only use the PS command of Unix/Linux to display other users.
|