shell is used to accept user commands and pass them to the os for execution
Basic Navigation and Viewing#
pwd#
pwd displays the absolute path of the current directory
ls#
list lists the files in the current directory
Parameters:
-l
lists the attributes of each file, usually abbreviated as ll
drwxr-xr-x@ 2 fling staff 64 5 24 15:42 test
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | | | | | |
| | | | | | | | | | | | └─ File/Directory Name
| | | | | | | | | | | └─────── Last Modified Time
| | | | | | | | | | └────────── Last Modified Date (Day of Month)
| | | | | | | | | └──────────── Last Modified Month
| | | | | | | | └──────────────── File Size (Bytes)
| | | | | | | └────────────────────── Group
| | | | | | └──────────────────────────── Owner
| | | | | └──────────────────────────────── Number of Hard Links
| | | | └────────────────────────────────── Extended Attributes Flag - @ (macOS specific)
| | | └──────────────────────────────────── Others' Permissions
| | └─────────────────────────────────────── Group's Permissions
| └────────────────────────────────────────── Owner's Permissions
└──────────────────────────────────────────── File Type
Attribute Explanation:
- File Type:
-
: Regular file (e.g.,1.txt
)d
: Directory (e.g.,test
)l
: Symbolic linkc
: Character device fileb
: Block device files
: Socket filep
: Named pipe
- File Permissions:
- Divided into three groups, each with three characters, representing the permissions for the file owner, group users, and other users
- Each group of characters is
rwx
, representing read, write, and execute permissions; if there is no corresponding permission, it is replaced with-
. For simplicity,r\w\x
is assigned numbers4\2\1
- Number of Hard Links:
- For files, it indicates how many filenames point to this file's
inode
- For directories, it is usually 2 + the number of subdirectories, as each directory has
.
pointing to itself and..
pointing to the parent directory
- For files, it indicates how many filenames point to this file's
-a
lists all files, including hidden files
. .. .env 1.txt test
cd#
c changes into a directory
As mentioned earlier, ..
represents the parent directory
cd -
represents going back to the last location
tree#
tree []
displays the structure of the directory []
File and Directory Operations#
touch#
touch updates the timestamp, but can also create files
mkdir#
mkdir creates a directory
cat#
cat outputs the contents of a file
less#
less views a file, can scroll up and down, press q
to return
Only reads and displays the portion needed for the screen
g
for the beginning, G
for the end
%
or p
for percentage jump
/pattern
for forward search, ?pattern
for backward search, n
for next match, N
for previous match
F
continuously monitors new content added to the end of the file, similar to tail -f
more#
more views a file, cannot scroll up, press q
to return
Loads the entire file into memory before displaying
head#
head outputs the beginning of a file
head --lines=n file
outputs the first n
lines of file
tail#
tail outputs the end of a file
tail --lines=n file
outputs the last n
lines of file
tail -f
continuously views subsequent content
cp#
cp source destination
copies files or directories
mv#
mv source destination
moves, can also be used for renaming
rm#
rm deletes
rm -r
recursively deletes, used for directories
ln#
ln links
ln -s source destination
creates a symbolic link, does not store the file, just a shortcut
ln source destination
creates a hard link, pointing to the source file, sharing the same inode, can be considered a reference, can only point to files
Permissions and Searching#
chmod#
modifies permissions
u\g\o
represents the three groups of permissions
chmod u+x file
grants execute permission to the owner
Can also be modified using numbers, such as chmod 744 file
file#
file identifies the file type, e.g., 1.txt: ASCII text
where/which/whereis#
where finds out where the file is located
Output, Piping, and Editing#
echo#
echo outputs to the command line, note that special characters should be enclosed in " "
>
redirects standard output to a file, e.g., echo "1" > 1.txt
>>
appends to a file
<
redirects standard input from a file
pipe#
command1 | command2 | command3
uses the standard output of one command as the standard input of another
ls -l | grep "*.txt"
cat server.log | grep "ERROR" | less
nano#
Crtl+X
to exit, Crtl+O
to save
vim#
In command mode, :wq
to save and exit, :q!
to force exit, [n]yy
to copy n lines, [n]p
to paste n times to the next line of the cursor, dd
to delete
In command mode, input i\a\o\I\A\O
to enter insert mode, in insert mode, esc
to return to command mode
In insert mode, ^
can jump to the beginning of the line, $
can jump to the end of the line
In command mode, input :
to enter line mode, in line mode, esc\enter
to return to command mode
Variables and Wildcards#
shell#
Defining a variable variable=value
is done, and you can use echo ${variable}
to view it; the braces are used to prevent ambiguity
Forward trimming, e.g., ff=week01
, echo ${ff#week}
outputs 01
Trim from the end %
?
represents a placeholder for one character, *
represents a placeholder for any character
for i in $(seq 1 10)
sequence from 1 to 10
for ((i=0;i<10;i++))
C-style loop
Batch renaming
for> do
for> mv ${d} chapter${d#week}
for> done
This article is synchronized and updated to xLog by Mix Space
The original link is https://blog.chanler.dev/posts/tech-learn/linux-command