Learn about the Linux file directory

Linux uses a forward slash (/) instead of a backslash (\) to divide directories in a file path. The Linux file system structure evolved from the Unix file structure. Common Linux top-level file directories:

directory use
/ The root directory of the virtual directory. Files are not normally stored here
/bin Binary directory for many user-level GNU tools
/boot The startup directory is used to store the startup files
/etc System configuration file directory
/home Home directory, where Linux creates user directories
/lib Library directory, store system and application library files
/media Media directory, a common mount point for removable media devices
/mnt Mount directory, another common mount point for removable media devices
/opt An optional directory for storing third-party software packages and data files
/proc Process directory, which stores information about existing hardware and current processes
/root Home directory of user root
/sbin System binary directory, which houses many GNU administrator level tools
/run Run directory, which stores runtime data when the system is running
/srv A service directory that stores files related to local services
/sys System directory, storage system hardware information related files
/tmp Temporary directory in which temporary working files can be created and deleted
/usr The user binary directory, where a large number of user-level GNU tools and data files are stored
/var Mutable directory for storing files that change frequently, such as log files

macOSThe system’s file directory serves a similar purpose:

Directory traversal

CD Destination switches the shell session to another directory. The CD command takes a single parameter destination, which specifies the name of the directory to which you want to switch. If no target path is specified for the CD command, it switches to the user home directory: [/users/ user name]. The destination parameter can be expressed in two ways: either using an absolute file path or using a relative file path. Special characters representing relative file paths:

  • Single point operator (.) : indicates the current directory.
  • Two point operator (.), representing the parent directory of the current directory.
Switch to the absolute file path
cd /usr
Change to bin in the current directory.
cd ./bin
/usr/bin/sbin /usr/bin/sbin
cd ../sbin
Sequential parent directory
cd. /.. /devSwitch to the user home directory
cd ~
# Check the current working directory
pwd
Display a list of files in the directory
ls
# check the inode number of the file to see if the file is independent.
ls -i
Distinguish between files and directories when displaying the contents below
ls -F
Show all files including hidden files
ls -a
Recursively enumerates files in subdirectories contained in the current directory
ls -R
ls -F -R
ls -FR # combination
Display additional information about files or directories under the current directory file
ls -l
ls -alF # combination
Copy the code

File filter

The ls command recognizes standard wildcards and uses them in filters for pattern matching:

  • Question mark (?) : represents a character;
  • An asterisk (*) : represents zero or more characters.

The question mark can be used to replace a single character anywhere in the filter string

#? matchingls te? t# * match
ls -l te*
#[] matches only test and teit
ls te[si]t 
# match te except a character? t
ls te[!a]t
# Letter range matches
ls -l f[a-i]ll 
Copy the code

File processing

  • touchCreating an empty file
touch file_one
Copy the code
  • cpCopy the file

The cp command consists of the following: cp source destination When both source and destination parameters are file names, the cp command copies the source file to a new file named destination. If the target file already exists, the cp command may not alert you to this. It is best to add -i to force the shell to ask if it needs to overwrite an existing file.

# copy file
cp one two
Copy files to a path
cp one app/
# copy app/two to current directory
cp app/two .
Copy the code
  • mvMove and rename files

In Linux, the renaming file is called moving. The mv command can move files and directories to another location or rename them. If the target file already exists, similar to the cp command, you can also use the -i parameter in the mv command.

# rename file one -> two
mv one two
# move file to another directory, two will not exist in the original directory after completion
mv two app/
mv -i three app/
Copy the code
  • rmDelete the file

In Linux, deleting is called removing, and the command to remove a file in the bash shell is rm.

# Remove file, will be prompted to remove.
rm -i app/one
# Remove files directly
rm app/one
# force remove
rm -f app/one
Copy the code

The directory processing

  • mkdirCreate a directory
Create a new_dir directory under the current directory
mkdir new_dir
Create a multi-level subdirectory under the current directory
mkdir -p new_dir/sub1/sub2
Copy the code
  • rmdirDelete the directory

Deleting directories can lead to a lot of misoperations, so the shell does everything it can to prevent us from getting into trouble. By default, the rmdir command deletes only empty directories.

# to create
mkdir new_dir
# switch
cd new_dir
# create file
touch one 
Return to the parent directory
cd.# delete
rmdir new_dir
# terminal error
rmdir: new_dir/: Directory not empty
Copy the code

Therefore, rmdir must be empty before deleting the directory.

# delete file
rm -i new_dir/one
# delete directory
rmdir new_dir
Copy the code

You can also run the rm -r command on a non-empty directory to go down to the directory, delete the files in the directory, and then delete the directory itself. This works well when you need to delete a large number of directories and files.

Each subdirectory hierarchy is reminded of a file check.
rm -ir new_dir
rm -ri new_dir
# delete commands that don't remind you
rm -r new_dir
Copy the code

The ultimate way to delete a directory and all its contents in one go is to use the rm command with -r and -f arguments.

# Force to delete files in subdirectories, and finally delete directories
rm -rf new_dir
Copy the code

Viewing file Contents

  • fileViewing file Types
Check the file type
file app.sh
# output
app.sh: ASCII text
# check directory
file new_dir
# output
new_dir: directory
# to check the PPTX
file XX.pptx
# to check the PDF
file XX.pdf
# output
XX.pdf: PDF document, version 1.4
# output
XX.pptx: Microsoft PowerPoint 2007+
Copy the code
  • catView the entire file
# display the contents of the file in app.sh
cat app.sh
# Display the contents of the file in app.sh and add the line number
cat -n app.sh
Copy the code

The main drawback of cat is that, once it’s run, you have no control over what follows. This leads to the more command, which allows paging. There is also an updated version of the more command, less, which is just a word game.

Page through text files
less page.js
more page.js
Copy the code
  • tailPartial view file

The tail command displays the last few lines of the file (the “tail” of the file). By default, it displays the last 10 lines of the file. There are files in the log file line 1 to 20

# default execution end view
tail log # display lines 11 to 20
# specifies to view the last three lines
tail -n 3 log # display lines 18 to 20
tail -3 log # display lines 18 to 20
Copy the code
  • headPartial view file

The head command displays the contents of the first lines of the file. By default, it displays the text of the first 10 lines of the file.

# Start view performed by default
head log # display lines 1 to 10
Specifies to view the first 3 lines
head -n 3 log # display lines 1 to 3
head -3 log # display lines 1 to 3
Copy the code
  • grepFile data search

The format of the grep command is grep [options] pattern [file]. Change log files from 1-10 to grep

Find the text associated with 'ne' from the log file
grep ne log Output one, nine
Copy the code

To perform a reverse search (output lines that do not match the pattern), add the -v argument:

Find text unrelated to 'ne' from the log file
grep -v ne log # Output the English of numbers except one and nine.
Copy the code

Displays the line number of the matched text, with the -n argument.

grep -n ne log Output: 1:one, 9:nine
Copy the code

Displays only how many rows contain matching patterns, using the -c argument.

grep -c ne log Output: 2
Copy the code

To specify multiple matching modes at the same time, use the -e argument to specify each mode.

# match s, ne
grep -e s -e ne log
grep -n -e s -e ne log # belt line number
# reverse matching
grep -v -e s -e ne log
Copy the code

Use regular matching patterns

grep [s] log
Copy the code

The resources

Linux command line and shell scripting