Operating Files in Linux

Linux manipulates files via the command line, including viewing, editing, copying, moving, deleting, creating, searching, matching, compressing, and uncompressing files, and it’s important to learn how to use the documentation.

Viewing


cat

1
2
$ # Output the entire contents of the FILE file
$ cat [OPTION] FILE

cat is short for concat*enate. The actual function of the cat utility is to concatenate multiple files and then output them. When there is only one file, cat will output that file directly, so the most common use of cat is to output a single file.

less

1
2
$ # Output the contents of a FILE file in an interactive window
$ less FILE

cat prints the entire contents to the terminal at once and exits, while less displays one page at a time and supports forward/backward scrolling, searching, etc. If you want to find a part of a large file, less is usually much more convenient than cat.

Similar commands to less: more, most.

Editing


Nano is a command-line text editor that comes with many machines and is more novice-friendly than vim and emacs.

1
$ nano FILE # Use nano to edit FILE files

Copying


1
2
3
4
5
6
7
8
9
$ # Copy the SOURCE file to the DEST file, the resulting file is DEST
$ cp [OPTION] SOURCE DEST
$ # Copy the SOURCE file to the DIRECTORY directory, SOURCE can be more than one file
$ cp [OPTION] SOURCE... DIRECTORY

$ # e.g.
$ cp file1 file3 # Make a copy of file1 to the same directory as file3
$ cp file1 file2 . /file/ # Copy file1, file2 files to the file directory in the same directory
$ cp -r file . /test/ # Copy the file folder and all its subfiles to the test folder in the same directory

Hard links and soft links

Hard links have the same inode as the source file and both point to the same location on disk. Deleting one of them does not affect the other.

Soft links have a different inode than the source file. Soft links save the path to the source file, and when accessing a soft link, the accessed path is replaced with the path to the source file, so accessing a soft link is also equivalent to accessing the source file. However, if the source file is deleted, the path saved by the soft link is invalid, and the soft link is therefore invalid as well1.

Moving


1
2
3
4
5
$ # Move the SOURCE file to the DEST file
$ mv [OPTION] SOURCE DEST

$ # Move SOURCE file to DIRECTORY directory, SOURCE can be multiple files
$ mv [OPTION] SOURCE... DIRECTORY

Similar to “cut” in Windows.

Deleting


1
2
3
4
5
6
7
8
9
$ # Delete FILE files, FILE can be multiple files.
$ # If you need to delete a directory, you need to delete it recursively with the -r option
$ rm [OPTION] FILE...

$ # e.g.
$ rm file1 # Delete the file1 file
$ rm -r test/ # Delete the test directory and all files under it
$ rm -rf test1/ test2/ file1.txt # Delete the files, directories test1/, test2/, file1.txt. Among them, these files or directories may not exist, be write-protected, or not have permission to read or write

Creating


Creating directories

1
2
3
4
5
6
$ # Create a directory named DIR_NAME
$ mkdir [OPTION] DIR_NAME...

$ # e.g.
$ mkdir test1 test2 # Create two directories with the names test1, test2
$ mkdir -p test1/test2/test3/ # Create path test1/test2/test3/

Creating files

1
2
$ # Create a file named FILE_NAME
$ touch FILE_NAME...

The stat command displays information about a file’s attributes.

What touch actually does is modify the access time (atime) and modification time (mtime) of the file, which can be treated as touching the file, causing its access and modification times to change. When the file does not exist, touch creates a new file.

Searching


1
2
3
4
5
6
7
$ # Search for files in PATH based on EXPRESSION
$ find [OPTION] PATH [EXPRESSION]

$ # e.g.
$ find . -name 'report.pdf' # Search for a file named report.pdf in the current directory
$ find / -size +1G # Search for files larger than 1G on the whole disk
$ find ~/ -name 'node_modules' -type d # Search for all folders named node_modules in the user directory

Pattern matching


The matching pattern for bash is called glob.

1
2
3
$ # e.g.
$ rm *.tar.gz # Remove all compressed files ending in tar.gz
$ mv -r *. [ch] /path # Move all code ending in .c or .h in the current and subdirectories to /path

Be sure to make sure you type correctly before using wildcards, otherwise serious consequences may occur (e.g. rm -rf * will delete all files in the current directory).

Compressing and uncompressing


Using tar to archive and compress files

1
2
3
4
5
6
7
$ # The command format is as follows, please refer to the following sample usage to understand how to use
$ tar [OPTIONS] FILE...

$ # e.g.
$ tar -c -f target.tar file1 file2 file3 # Package file1, file2, file3 as target.tar
$ tar -x -f target.tar -C test/ # Extract the files in target.tar to the test directory
$ tar -cz -f target.tar.gz file1 file2 file3 # Pack file1, file2, file3 and compress them using gzip to get the compressed file target.tar.gz

As with most Linux commands, the tar command can combine parameters. For example, the following command is equivalent:

1
2
3
$ tar -c -z -v -f target.tar test/
$ tar -czvf target.tar test/
$ tar -f target.tar -czv test/

The suffix name does not determine the file type, but it can help one to identify the possible file types of this file and thus choose the appropriate opening method.

Documentation


man commands

1
2
3
$ # Call up the documentation for the tar and ls commands
$ man tar
$ man ls

It shows the meaning of each parameter of the software, the meaning of the exit value, the author, etc. It is large and comprehensive. But generally less with the use of sample, you need to splice the software parameters according to their own needs, not conducive to quickly get started.

tldr

A few simple examples allow users to quickly understand how to use the software.

1
$ tldr COMMAND

Note that if this error is reported:

1
mkdir:Unable to create directory "/home/lzy/.tldr": file already exists

The solution is:

1
2
3
4
$ sudo apt-get update
$ sudo apt-get install tldr
$ mkdir -p ~/.tldr/tldr
$ sudo git clone https://github.com/tldr-pages/tldr.git ~/.tldr/tldr

Appendix


Linux 101 - chap 03: 软件安装与文件操作

[1] 阮一峰的网络日志 - 理解inode

Jan 2023 Installing Software in Linux

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×