Overview
The management of files or directories mainly includes creation, deletion, query, and movement. mkdir, rm, mv
File query, mainly using the powerful find command.
Create and delete
- Create a directory: mkdir
- remove: rm
- Remove non-empty directories: rm -rf
- move/rename: mv
- copy: cp
- Copy directory: cp -r
Directory switching
- Find the file directory location: cd
- Change to the last working directory: cd -
- Change to home directory: cd or cd ~
- Show current path: pwd
list directory entries
- Display the files of the current project: ls
- Sorted by time, displayed as a list: ls -lrt
- Add the id number in front of each file item: ls | cat -n
Generally, we will add aliases in .bashrc to simplify the use of commands.
Find directories and files
- Find a file or directory: find ./ -name "test*"
find is a real-time search. If you need a faster query, you can try locate; locate will build an index database for the file system. If there is a file update, you need to periodically execute the update command to update the index database:
- locate string
- updatedb
View the contents of a file
View file: cat vi head tail more
- Display the line number at the same time: cat -n
- Display list contents by page: ls -al | more
- View the first 10 lines of the file: head -10 test.cc
- View the last 5 lines of the file: tail -5 test.cc
- See the difference between the two files: diff test.cc test_old.cc
- Dynamically display the latest information in the text: tail -f desing.log
Find the contents of a file
Use grep egrep to query the contents of the file, the usage of grep is described in a separate article
grep 'main' test.cc
Modifying file and directory permissions
- Modify the owner of the file: chown
- Change the permissions of read, write, execute, etc. of the file: chmod
- Recursive subdirectory modification: chown -R / chmod -R
- Increase the executable permission of the file: chmod a+x test
Add aliases to files, soft links/hard links
- Hard link: ln test hlink
- 软连接: ln -s test slink
管道
- 批处理命令连接执行,管道使用 |
- 串联使用: 分号 ;
- 前面成功,则执行后面一条,否则不执行: &&
- 前面失败,则执行后面一条: ||
重定向
- 将标准输出和错误输出到一个文件: ls test* > list 2>&1 等价于 ls test* &> list
- 清空文件: > test
- 重定向到文件末尾: echo “Hello world” >> test