Learn basic Linux commands Part 3
Let's remember the necessary commands a little more. I wonder if it will end this time.
Delete files rm
Use rm to delete the file.
rm FileName
You can use the -f option to delete a file without confirming the deletion. "
rm -f FileName
Please use rm with caution. Be very nervous when deleting files with the rm command. Once you delete an important file, it cannot be restored. It is better not to hit it with an easy feeling, and I think it is better to check it about 3 times and do it with excitement.
Creating a directory mkdir
Use mkdir to create a directory.
mkdir dir1
Creating a multi-level directory
You can use the -p option to create multiple levels of directories at once.
mkdir -p dir1/dir2
Delete directory rmdir
Use rmdir to delete the directory.
rmdir directory name
Rmdir will succeed only if the directory is empty.
Use the -r option of the rm command to delete the contents of the directory as well.
rm -r DirectoryName
To delete without inquiring whether to delete, use the -f option in combination.
rm -rf DirectoryName
Please do this very carefully as well. It's a command that system administrators need to be prepared to type, as the contents of the directory will be messed up.
Shut down the system shutdown
Use shutdown to shut down the system.
shutdown -h now
The -h option is an option for shutting down the system. If you specify now , the shutdown will start immediately.
Reboot the system reboot
Use the reboot command to reboot the system.
reboot
Display the date
Use the data command to display the date.
date
If you want to display the time every second, you can write as follows.
while true do date sleep 1 done
You can stop it with ctrl + c. sleep is a command that does nothing and waits for the specified number of seconds. while can be repeated in shell syntax.
Compress files gzip
Compression is an operation to reduce the size of the file. Use the gzip command for compression.
gzip FileName
The compressed file has the extension .gz at the end.
For example, if you compress development.log, it will become development.log.gz.
Unzip the compressed file gunzip
Use gunzip to restore the compressed file.
gunzip CompressedFileName
Archive the directory tar
Archiving is the consolidation of multiple files contained in a directory into a single file. Use the tar command.
tar fc ArchiveName DirectoryName
f should be used when the target is a directory. c must be specified when archiving. The archive name is usually "Direct name.tar".
The following is a sample.
tar cf dir1.tar dir1
You can also compress with gzip at once. In that case, specify the z option.
tar cfz dir1.tar.gz dir1
Extract the archive
Use the x option to extract the archive.
tar xf ArchiveName
The following is a sample.
tar xf dir1.tar
Files that have been archived and then compressed with gzip can be expanded to the state of the directory by specifying the z option at the same time.
tar xfz dir1.tar.gz