System

1. man command —— show the manual for command

$ man whereis

2. whereis app —— show possible locations of app

$ whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz

3. which app —— show which app will be run by default

$ which gcc
/usr/bin/gcc

4. whoami —— show your username (who you are logged in as)

$ whoami
airobot

File/Directory Basics

5. pwd —— show the present working directory

$ pwd
/home/airobot/Desktop/dev

6. cd

cd dir —— change directory to dir

cd .. —— go up one directory

cd / —— go to the root directory

cd ~ —— go to your home directory

cd - —— go to the last directory you were just in

File  Viewing

7. cat —— output the contents of file

8. head —— output the first 10 lines of file

9. tail —— output the last 10 lines of file

10. nl —— number lines

File Properties/Permissions

11. file —— identify file types

12. stat —— display file attributes

13. chmod ugo file —— change permissions of file to ugo, where u is the user's permission, g is the group's permissions, and o is the everyone else's permission. The walues of u, g, and o can bu any number between 0 and 7. (4: read, 2: write, 1: execute)

e.g.

chmod 777: read, write, execute for all (参考上图中给出的例子)

chmod 755: rwx for owner, everyone else can rx

chmod 600: you can read and write

chmod 644: you can read and wirte, and everyone else can only read

14. chgrp —— change file group

15. chown —— change file owner

IO Redirection

16. cat > file —— places standard input to file

例如,在下图中,我们首先在命令上输入 cat > my_file,然后系统会自动在当前文件夹下创建一个名为my_file的文件,然后我们在Terminal上输入的文字将被写入my_file中,直到我们在键盘上输入CTRL + d 时返回。这时我们打开my_file文件,可以发现之前的内容已经被成功写入了。

17. cmd > file —— Standard output (stdout) of cmd to file

18. cmd >> file ——  Append stdout to file

Process Management

19. ps —— show snapshot of processes

ps命令用于报告当前系统的进程状态。可以搭配kill指令随时中断、删除不必要的程序。ps命令是最基本同时也是非常强大的进程查看命令,使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等,总之大部分信息都是可以通过执行该命令得到的。

20. top —— show real time processes

 top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具。通过top命令所提供的互动式界面,用热键可以管理。

21. kill pid —— kill process with id pid

22. pkill name —— kill process with name name

23. killall name —— kill all processes with names beginning name

Searching

24. locate file —— find all instances of file using indexed database built from the updatedb command. Much faster than find

25. grep pattern files —— search for pattern in files

grep -i : case insensitive search

grep -o : only show matched part of file

26. grep -r pattern dir —— search recursively for pattern in dir

27. grep -rn pattern dir —— search recursively for pattern in dir and show the line number found

28. command | grep pattern —— search for pattern in the output of command

Pipes and Command Lists

29. cmd1 | cmd2 —— stdout of cmd1 to cmd2

30. cmd1 |& cmd2 —— stderr of cmd1 to cmd2

31. cmd1 ; cmd2 —— run cmd1 then cmd2

32. cmd1 && cmd2 —— run cmd2 if cmd1 is successful

33. cmd1 || cmd2 —— run cmd2 if cmd1 is unsuccessfull

Shortcuts

CTRL+c —— stop current command

CTRL+w —— erases one word in the current line

CTRL+u —— erases the whole line

CTRL+a —— move cursor to the beginning of line

CTRL+e —— move cursor to the end of line

CTRL+b —— move cursor backward one character

CTRL+f ——  move cursor forward one character

ALT+b —— move cursor backward one word

ALT+f —— move cursor forward one word

统计文件夹下的文件数目

Linux下有三个命令:lsgrepwc。通过这三个命令的组合可以统计目录下文件及文件夹的个数。

  • 统计当前目录下文件的个数(不包括目录)
ls -l | grep "^-" | wc -l
  • 统计当前目录下文件的个数(包括子目录)

ls -lR| grep "^-" | wc -l

  • 查看某目录下文件夹(目录)的个数(包括子目录)

ls -lR | grep "^d" | wc -l

解析:
    ls -l

长列表输出该目录下文件信息(注意这里的文件是指目录、链接、设备文件等),每一行对应一个文件或目录,ls -lR是列出所有文件,包括子目录。

    grep "^-"
过滤ls的输出信息,只保留一般文件,只保留目录是grep "^d"。

    wc -l
统计输出信息的行数,统计结果就是输出信息的行数,一行信息对应一个文件,所以就是文件的个数。

压缩与解压缩(zip 与 unzip)

1. zip

基本语法

zip [options] [file_name.zip] [files_names]

例如:

zip myfile.zip first.txt second.txt third.txt

[options]的常见选项:

-d

(Remove files from the archive):
This option allows you to remove specific files from a zip archive. After creating a zip file, you can selectively remove files using the -d option.

-r

(Recursively zip a directory):
The -r option allows you to recursively zip a directory and its files. It includes all the files present in the specified directory and its subdirectories in the resulting zip archive.

例如:

zip -r [file_name.zip] [directory_name]
-x

(Exclude files from the zip):
Using the -x option, you can exclude specific files from being included in the zip archive. This is useful when you want to zip all files in a directory but want to exclude certain unwanted files.

例如:

zip -r myfile.zip . -x  a.txt

其中 . 表示当前文件夹,-x a.txt表示排除 a.txt文件

最后,-P 可以用来创建一个压缩密码。例如

zip -e -P [my_password] myfile.zip first.txt second.txt

By using option `-P` followed by the password we want to use. Also, we use `-e` to encrypt our zip archive, so whenever a user extracts the archive file, a prompt will come to enter the password.

2. unzip

例如:

unzip filename.zip -d /directory-path

By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory).

The ‘-l’ option with unzip command is used to list the contents of a zip file as follows,例如:

unzip -l file-name.zip

如要解压多个文件,可以考虑使用通配符,例如:

unzip '*.zip'

对于加密文件,使用:

unzip -P Password file-name.zip

参考

http://gnulinux.guru/bash_cheatsheet.pdf

http://man.linuxde.net/

Linux统计文件夹下的文件数目

Logo

更多推荐