Linux Commands for Managing Files and Directories

Linux Commands for Managing Files and Directories

pwd——Prints the current path of the user, print work directory, used in text mode

cd——Changes the current directory, change directory

Format: cd <directory_name>

[root@localhost~]# cd.. Go back to the parent directory

[root@localhost~]# cd Go to the user’s home directory

[root@localhost~]# cd/home Switch to the home directory using the path

ls——Displays the contents of the specified directory, list directory

Format: ls [options][directory or file]

Option Meaning
-a Lists all files in the directory, including hidden files
-l Lists details in the directory, including permissions, owner, group, size, creation date, whether the file is linked, etc.
-r Reverses the order, lists the contents of the directory from back to front
-R Recursively lists all contents in all subdirectories of the current directory
-s Size: data blocks

ex. ls -l 1.txt

[root@localhost~]# ls /home Displays the contents of the specified directory /home

[root@localhost~]# ls -l Displays detailed contents of the current directory

[root@localhost~]# ls -a Displays all files in the current directory, including hidden files

cat——Displays the contents of a text file

Syntax: cat

ex. cat 1.txt

touch——Creates a text file (cannot insert content)

Syntax: touch

ex. touch 12.txt

grep——Searches for a specific string in a pile of files, grep search is case-sensitive

ex. grep money test.txt Searches for the string ‘money’ in test.txt

cp——Copies files, copy

Format: cp [source_file][destination_file]

[root@localhost~]# ls

[root@localhost~]# cp hello.txt file1.txt Copies hello.txt and names it file1.txt

Option Meaning
-i Interactive: if the file will overwrite the target file, it prompts for confirmation
-r Recursive: this option will copy the entire directory, subdirectories, and more
-v Verbose: displays the progress of the file copying

[root@localhost~]# cp hello.txt /home/myfile.txt

Copies the file hello.txt from the current directory to the path /home and names it myfile.txt

ex. cp -r script /home

mv——Can move files or directories

Format: mv [source][destination]

Option Meaning
-i Interactive: if the selected file will overwrite the target file, it prompts for confirmation
-f Force: moves the file without prompting
-v Verbose: displays the progress of the file moving

ex. mv 4.txt /home

mv 3.txt /home/33.txt

mv /home/33.txt /etc

mkdir——Creates a directory, make directory

Format: mkdir [directory_name1][directory_name2]……

ex. mkdir dir2 Creates a directory

mkdir aa bb cc Creates multiple directories at once

mkdir -p a/b Only one directory can be nested in a directory

mkdir -p aa/bb/cc

rmdir——Removes empty directories, remove directory

Format: rmdir [options][directory_name1][directory_name2]……

ex. rmdir aa bb cc

rmdir -p aa/bb/cc Removes nested directories

rm——Removes files, remove

Format: rm [options][file]

ex. rm 2.txt , rm /etc/33.txt

rm -v /etc/test.txt , rm -r a

Option Meaning
-i Interactive: prompts for confirmation to delete
-f Force: replaces interactive mode, does not prompt for confirmation to delete
-v Verbose: displays the progress of file deletion
-r Recursive: deletes a directory and all its files and subdirectories

Leave a Comment