1. Basic operation of files
1.1 File creation
touch command
touch
Timestamp used to create an empty file or update a file.
- usage:
# Create an empty filetouch # Create multiple files at the same timetouch # Update file timestamptouch -a
cat and echo
-
cat
Create a file:
# Create and write contentcat > <<EOF This is a test file. EOF
-
echo
Write content:
echo "Hello, World!" >
1.2 File Deletion
rm command
rm
Used to delete files or directories.
- usage:
# Delete a single filerm # Delete multiple filesrm # Forced deletionrm -f # Delete the directory and its contentsrm -r directory_name
- Notice:
rm -rf /
It is a high-risk operation that may lead to loss of system files and needs to be used with caution.
1.3 File renaming and moving
mv command
mv
Used to rename files or move files.
- usage:
# Rename the filemv old_name.txt new_name.txt # Move the file to the specified directorymv /path/to/directory/ # Rename and movemv /path/to/directory/new_file.txt
2. View and edit file content
2.1 View file content
cat command
cat
It is the most commonly used tool to view file content.
- usage:
# View the entire file contentcat # Display with line numbercat -n
less command
less
Used to view large file contents by paging.
usage:
less
-
navigation:
- Turn the page down:
Space
orf
- Turn the page up:
b
- quit:
q
- Turn the page down:
tail and head commands
-
tail
View the content at the end of the file:
tail # View the last 20 linestail -n 20
-
head
View the contents of the beginning of the file:
head # View the first 10 lineshead -n 10
2.2 Editing files
nano and vim editors
-
nano
Simple to operate, suitable for beginners:
nano
-
vim
Powerful function, suitable for advanced users:
vim
3. File permission management
3.1 View file permissions
ls -l command
usage:
ls -l
Output format:
-rw-r--r-- 1 user group 1024 Dec 6 12:34
- Column 1: Permission flag (
r
Readable,w
Writable,x
Executable). - Second column: Number of links.
- Columns 3 and 4: Users and Groups to which they belong.
- Subsequently: file size, modification time, file name.
- Column 1: Permission flag (
3.2 Modify file permissions
chmod command
usage:
# Add permissionschmod +x # Delete permissionschmod -w # Set permissionschmod 644
-
Symbol and numerical patterns:
-
r=4
,w=2
,x=1
。 -
644
Indicates that the owner can read and write, and the group and other users can only read.
-
3.3 Modify the user or group to which the file belongs
chown command
- usage:
# Change the file ownersudo chown user # Change file owner and groupsudo chown user:group
4. File search and positioning
4.1 find command
find
Used to search for files based on conditions.
- usage:
# Search by namefind /path -name "" # Search by sizefind /path -size +100M # Search by modification timefind /path -mtime -7
4.2 locate command
locate
Use indexes to quickly locate files.
- usage:
# Search for fileslocate
4.3 grep command
grep
Search for the specified mode in the file content.
- usage:
# Search for wordsgrep "word" # Recursive search directorygrep -r "pattern" /path
5. File compression and decompression
5.1 tar command
tar
Used for archive files.
- usage:
# Compress filestar -czvf file1 file2 # Unzip the filetar -xzvf
5.2 zip and unzip
Compressed files:
zip file1 file2
Unzip the file:
unzip
6. File transfer
6.1 Local Transfer
cp
6.2 Network transmission
-
scp
Order:
scp user@remote:/path
7. Actual case of file operation
7.1 Statistics the number of log files
wc -l /var/log/syslog
7.2 Delete log files for more than 30 days
find /var/log -type f -mtime +30 -exec rm {} \;
7.3 Find large files
find / -type f -size +1G
7.4 Batch modification file permissions
find /path -type f -name "*.sh" -exec chmod +x {} \;
Summarize
Linux provides a rich variety of file operation commands, from file creation, editing, permission management, and then searching and compression, covering all aspects of daily management. Through the systematic learning of this article, readers can not only quickly master common commands, but also apply them to actual work to improve efficiency and management capabilities.
The above is the detailed explanation and actual combat of Linux file operation commands. For more information about Linux file operation, please pay attention to my other related articles!