Skip to content

Commit 3248e4d

Browse files
os: command-line commands section
1 parent 30c220a commit 3248e4d

File tree

6 files changed

+273
-1
lines changed

6 files changed

+273
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- dev: add scheduled stale issue & pull request check (monthly)
1919
- os: install sudo & add user to sudo group
2020
- os: starship prompt in command-line shell
21+
- os: command-line commands section
2122

2223
### Changed
2324

File renamed without changes.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# File System
2+
3+
4+
- [List files and directories](#list-files-and-directories)
5+
- [find files and contents]()
6+
7+
8+
9+
## List files and directories
10+
11+
`ls` command lists the contents of the current directory or the specified path.
12+
13+
`ls -l` shows the meta data of files, e.g. ownership, permissions, etc.
14+
15+
`ls -a` includes hidden dot-files in the list, e.g. `.git`
16+
17+
18+
`tree` shows a recursive list of files and directories, e.g. show the whole contents of a code project.
19+
20+
21+
## Find files and contents
22+
23+
24+
The find command is one of the most useful Linux commands, especially when you're faced with the hundreds and thousands of files and folders on a modern computer. As its name implies, find helps you find things, and not just by filename.
25+
26+
Whether you're on your own computer or trying to support someone on an unfamiliar system, here are 10 ways find can help you locate important data.
27+
28+
[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]
29+
1. Find a single file by name
30+
31+
When you know the name of a file but can't remember where you saved it, use find to search your home directory. Use 2>/dev/null to silence permission errors (or use sudo to gain all permissions).
32+
33+
$ find / -name "Foo.txt" 2>/dev/null
34+
/home/seth/Documents/Foo.txt
35+
36+
2. Find a single file by approximate name
37+
38+
If you can't remember the exact name of the file, or you're not sure whether you capitalized any characters, you can do a partial and case-insensitive search like this:
39+
40+
$ find / -iname "*foo*txt" 2>/dev/null
41+
/home/seth/Documents/Foo.txt
42+
/home/seth/Documents/foo.txt
43+
/home/seth/Documents/foobar.txt
44+
45+
3. Find everything
46+
47+
The ls -R command lists the contents of a directory recursively, meaning that it doesn't just list the target you provide for it, but also descends into every subdirectory within that target (and every subdirectory in each subdirectory, and so on.) The find command has that function too, by way of the -ls option:
48+
49+
$ find ~/Documents -ls
50+
3554235 0 drwxr-xr-x [...] 05:36 /home/seth/Documents/
51+
3554224 0 -rw-rw-r-- [...] 05:36 /home/seth/Documents/Foo
52+
3766411 0 -rw-rw-r-- [...] 05:36 /home/seth/Documents/Foo/foo.txt
53+
3766416 0 -rw-rw-r-- [...] 05:36 /home/seth/Documents/Foo/foobar.txt
54+
55+
Notice that I don't use 2>/dev/null in this instance because I'm only listing the contents of a file path within my home directory, so I don't anticipate permission errors.
56+
4. Find by content
57+
58+
A find command doesn't have to perform just one task. In fact, one of the options in find enables you to execute a different command on whatever results find returns. This can be especially useful when you need to search for a file by content rather than by name, or you need to search by both.
59+
60+
$ find ~/Documents/ -name "*txt" -exec grep -Hi penguin {} \;
61+
/home/seth/Documents/Foo.txt:I like penguins.
62+
/home/seth/Documents/foo.txt:Penguins are fun.
63+
64+
[ Learn how to manage your Linux environment for success. ]
65+
5. Find files by type
66+
67+
You can display files, directories, symlinks, named pipes, sockets, and more using the -type option.
68+
69+
$ find ~ -type f
70+
/home/seth/.bash_logout
71+
/home/seth/.bash_profile
72+
/home/seth/.bashrc
73+
/home/seth/.emacs
74+
/home/seth/.local/share/keyrings/login.keyring
75+
/home/seth/.local/share/keyrings/user.keystore
76+
/home/seth/.local/share/gnome-shell/gnome-overrides-migrated
77+
[...]
78+
79+
As long as you're using the GNU version of find, you can include multiple file types in your search results:
80+
81+
$ find ~ -type f,l -name "notebook*"
82+
/home/seth/notebook.org
83+
/home/seth/Documents/notebook-alias.org
84+
85+
6. List just directories
86+
87+
A shortcoming of the ls command is that you can't filter its results by file type, so it can be noisy if you only want a listing of directories in a path. The find command combined with the -type d option is a better choice:
88+
89+
$ find ~/Public -type d
90+
find ~/Public/ -type d
91+
/home/seth/Public/
92+
/home/seth/Public/example.com
93+
/home/seth/Public/example.com/www
94+
/home/seth/Public/example.com/www/img
95+
/home/seth/Public/example.com/www/font
96+
/home/seth/Public/example.com/www/style
97+
98+
7. Limit listing results
99+
100+
With hundreds of files in a default user directory and thousands more outside of that, sometimes you get more results from find than you want. You can limit the depth of searches with the -maxdepth option, followed by the number of directories you want find to descend into after the starting point:
101+
102+
$ find ~/Public/ -maxdepth 1 -type d
103+
/home/seth/Public/
104+
/home/seth/Public/example.com
105+
106+
8. Find empty files
107+
108+
Sometimes it's helpful to discover empty files as a way to declutter:
109+
110+
$ find ~ -type f -empty
111+
random.idea.txt
112+
113+
Technically, you can use find to remove empty files, but programmatic removal of files is dangerous. For instance, if you forget to include -type f in a search for empty files, you get directories in your results. By adding a delete flag, you would remove potentially important directory structures.
114+
115+
It's vital to compose your find command and then verify the results before deleting. Furthermore, a misplaced delete flag in find can delete results before qualifying them (in other words, you can delete directories in a command intended to delete only files by placing the delete flag before the type flag).
116+
117+
I prefer to use xargs or Parallel and a trash command on the rare occasion that I remove files with find.
118+
9. Find files by age
119+
120+
The -mtime option allows you to limit a search to files older than, but also files newer than, some value times 24.
121+
122+
$ find /var/log -iname "*~" -o -iname "*log*" -mtime +30
123+
124+
The + before the -mtime number doesn't mean to add that number to the time. It's a conditional statement that matches (in this example) a value greater than 24 times 30. In other words, the sample code finds log files that haven't been modified in a month or more.
125+
126+
To find log files modified within the past week, you can use the - conditional:
127+
128+
$ find /var/log -iname "*~" -o -iname "*log*" -mtime -7
129+
/var/log/tallylog
130+
/var/log/cups/error_log
131+
/var/log/cups/access_log
132+
/var/log/cups/page_log
133+
/var/log/anaconda/anaconda.log
134+
/var/log/anaconda/syslog
135+
/var/log/anaconda/X.log
136+
[...]
137+
138+
You already know about the -ls flag, so you can combine that with these commands for clarity:
139+
140+
$ find /var/log -iname "*~" -o -iname "*log*" -mtime -7 -ls
141+
-rw------- 1 root root 0 Jun 9 18:20 /var/log/tallylog
142+
-rw------- 1 root lp 332 Aug 11 15:05 /var/log/cups/error_log
143+
-rw------- 1 root lp 332 Aug 11 15:05 /var/log/cups/access_log
144+
-rw------- 1 root lp 332 Aug 11 15:05 /var/log/cups/page_log
145+
-rw------- 1 root root 53733 Jun 9 18:24 /var/log/anaconda/anaconda.log
146+
-rw------- 1 root root 835513 Jun 9 18:24 /var/log/anaconda/syslog
147+
-rw------- 1 root root 21131 Jun 9 18:24 /var/log/anaconda/X.log
148+
[...]
149+
150+
10. Search a path
151+
152+
Sometimes you know the directory structure leading up to a file you need; you just don't know where the directory structure is located within the system. To search within a path string, you can use the -ipath option, which treats dots and slashes not as regex characters but as dots and slashes.
153+
154+
$ find / -type d -name 'img' -ipath "*public_html/example.com*" 2>/dev/null
155+
/home/tux/Public/public_html/example.com/font
156+
157+
Found it
158+
159+
The find command is an essential tool for a sysadmin. It's useful when investigating or getting to know a new system, finding misplaced data, and troubleshooting everyday problems. But it's also just a convenience tool.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Common CLI Commands
2+
3+
Console and output management:
4+
cat - Display file contents on the terminal.
5+
clear - Clear the terminal display.
6+
echo - Print text or variables in the terminal.
7+
top - Get information about running processes.
8+
Creating and exporting environment variables:
9+
env - Display all environment variables running on the system.
10+
export - Export environment variables.
11+
printenv - Print a particular environment variable to the console.
12+
source - Execute commands stored in a file from within the current shell, or refresh environment variables.
13+
Working with files and directories:
14+
cd - Change to another directory.
15+
cp - Copy the contents of the source directory or file to a target directory or file.
16+
find - Locate a file or directory by name.
17+
grep - Search for a string within an output.
18+
ls - List the contents of a directory.
19+
mkdir - Create directories.
20+
more - View and traverse the content of a file or stdout.
21+
mv - Move or rename files.
22+
pwd - Get the name of the present working directory.
23+
rm - Delete files or directories.
24+
tar - Extract and compress files.
25+
Accessing command-line help documentation:
26+
man - Access manual pages for all Linux commands.
27+
Working with networks on and from a Linux computer:
28+
curl - Get or post a file to or from the Internet according to a URL.
29+
ip - Gets the IP information for the physical or virtual machine.
30+
netstat - Get information about network connections and more.
31+
ssh - Establish a secure encrypted connection between two hosts over an insecure network.
32+
wget - Direct download files from the Internet.
33+
Process management:
34+
&& - Execute commands in a sequence.
35+
kill - Removes a running process from memory.
36+
ps - Display active processes.
37+
System control:
38+
poweroff - Shut down a computer.
39+
restart - Restart a computer.
40+
User management:
41+
whoami - Display the user ID.
42+
43+
Red Hat Developer cheat sheets give you essential information right at your fingertips so you can work faster and smarter. Easily learn new technologies and coding concepts and quickly find the answers you need.
44+
Excerpt
45+
find
46+
47+
sudo find <starting/directory> -name <file/directory name>
48+
49+
Copy snippet
50+
51+
Finds a file or directory by name.
52+
53+
Example:
54+
55+
The following command finds a file named hostname starting from the root (/) directory of the computer’s filesystem. Note that the command starts with sudo in order to access files restricted to the root user:
56+
57+
$ sudo find / -name hostname
58+
59+
/proc/sys/kernel/hostname
60+
61+
/etc/hostname
62+
63+
/var/lib/selinux/targeted/active/modules/100/hostname
64+
65+
/usr/bin/hostname
66+
67+
/usr/lib64/gettext/hostname
68+
69+
/usr/share/licenses/hostname
70+
71+
/usr/share/doc/hostname
72+
73+
/usr/share/bash-completion/completions/hostname
74+
75+
/usr/share/selinux/targeted/default/active/modules/100/hostname
76+
77+
/usr/libexec/hostname
78+
79+
Copy snippet
80+
grep
81+
82+
grep <search_expression> <input>
83+
84+
$ less -N ~/.bashrc
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Managing Software
2+
3+
Operating systems provide commands to manage software and there are general tools to manually install software and diagnose issues.
4+
5+
6+
## Package Managers
7+
8+
9+
=== "Debian Linux"
10+
11+
`apt`
12+
13+
14+
== "Arch Linux"
15+
16+
`pacman`
17+
18+
19+
## General tools
20+
21+
`which` shows the full path to a command's executable file
22+
23+
24+
`top`

mkdocs.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,13 @@ nav:
190190
- Bash: os/command-line/shell/bash.md
191191
- Zsh: os/command-line/shell/zsh.md
192192
- Fish: os/command-line/shell/fish.md
193+
- Starship prompt: os/command-line/shell/starship-prompt.md
194+
- Commands:
195+
- os/command-line/commands/index.md
196+
- Files: os/command-line/commands/file-system.md
197+
- Cron: os/command-line/commands/cron.md
193198
- Linux:
194199
- Overview: os/linux/index.md
195-
- Cron: os/cron.md
196200
- Debian: os/debian-linux.md
197201
- Ubuntu: os/ubuntu-linux.md
198202
- Hyprland: os/linux/hyprland.md

0 commit comments

Comments
 (0)