Basic Files Operations

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn to use some basic operation command that allows you to perform actions such as list files, change the current directory, and move/copy/delete files and directories.

Achievements

  • ls - list files and directories in the current directory.
  • clear - clear the terminal screen.
  • cd - change the current working directory.
  • cp - copy files and directories.
  • mv - move files and directories.
  • rm - remove files and directories.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/clear("`Screen Clearing`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") subgraph Lab Skills linux/clear -.-> lab-270248{{"`Basic Files Operations`"}} linux/cd -.-> lab-270248{{"`Basic Files Operations`"}} linux/ls -.-> lab-270248{{"`Basic Files Operations`"}} linux/cp -.-> lab-270248{{"`Basic Files Operations`"}} linux/mv -.-> lab-270248{{"`Basic Files Operations`"}} linux/rm -.-> lab-270248{{"`Basic Files Operations`"}} end

List Files and Directories

The ls command lists files and directories in the current directory. The following example shows how to list files and directories in the current directory.

ls

The default path of the current directory is ~/project in our environment. There is a file named sample1.txt in it.

alt text

Clear the Screen

clear is a command that clears the terminal screen. It is useful when you want to clear the screen to make it easier to read the output of a command.

Just run the following command:

clear

Now you can see the terminal screen is cleared. When you terminal screen is cleared, you can see the prompt again.

List Files in A Specific Directory

The following example shows how to list files and directories in the ~/Desktop directory.

ls ~/Desktop

~ is a shortcut for the current user's home directory. The default value is /home/labex In our environment.

Sample output:

code.desktop  gedit.desktop  gvim.desktop  xfce4-terminal.desktop

List With Details

The following example shows how to list files and directories in the ~/Desktop directory with details.

ls -l ~/Desktop

-l is a flag that tells the ls command to list files and directories with details.

Sample output:

total 28
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
……

List Hidden Files and Directories

The following example shows how to list files and directories in the ~ directory with details and hidden files.

Tips: Hidden files and directories are files and directories that start with a . (dot).

ls -la ~

-a is a flag that tells the ls command to list all files and directories, including hidden files and directories.

Sample output:

total 220
drwxr-x--- 1 labex labex   252 Jan 19 09:12 .
drwxr-xr-x 1 root  root     19 Jan 15 10:47 ..
-rw------- 1 labex labex   382 Jan 19 09:09 .ICEauthority
-rw------- 1 labex labex   118 Jan 19 09:09 .Xauthority
……

Sort by Size and List Hidden Files

The following example shows how to list files and directories in the ~ directory with details and hidden files and sort by size.

ls -laS ~

-S is a flag that tells the ls command to sort by file size.
Sample output:

total 220
-rw-rw-r-- 1 labex labex 50842 Jan 19 09:09 .zcompdump-65a9cbb8f979b6c975b91001-5.8.1
-rw-rw-r-- 1 labex labex 50842 Jan 15 10:50 .zcompdump-buildkitsandbox-5.8.1
……

Details Human-Readable

The following example shows how to list files and directories in the ~ directory with details and hidden files in reverse order and sort by size and human-readable.

ls -laSh ~

-h is a flag that tells the ls command to print sizes in human-readable format.

Sample output:

total 220K
-rw-rw-r-- 1 labex labex  50K Jan 19 09:09 .zcompdump-65a9cbb8f979b6c975b91001-5.8.1
-rw-rw-r-- 1 labex labex  50K Jan 15 10:50 .zcompdump-buildkitsandbox-5.8.1
-rw-rw-r-- 1 labex labex  49K Jan 19 09:09 .zcompdump
……

Change Working Directory

cd is a command that allows you to change the current working directory.

The following example shows how to change the current working directory to the ~/Desktop directory.

cd ~/Desktop
ls

Output:

code.desktop  gedit.desktop  gvim.desktop  xfce4-terminal.desktop

Change to the Parent Directory

The following example shows how to change the current working directory to the parent directory.

cd ..
ls

.. is a shortcut for the parent directory.

Output:

Code  Desktop  golang  project

Change to the Root Directory

The following example shows how to change the current working directory to the root directory.

cd /
ls

/ is the root directory.

Output:

bin  boot  code-server_4.18.0_amd64.deb  data  dev  etc  home  lib  lib32  lib64  libx32  mariadb_repo_setup  media  mnt  opt  proc  root  run  sbin  snap  srv  sys  tmp  usr  var

Change to the Previous Directory

The following example shows how to change the current working directory to the previous working directory.

cd -
ls

- is a shortcut for the previous working directory.

Output:

Code  Desktop  golang  project

Change to the Home Directory

The following example shows how to change the current working directory to the home directory.

cd ~
ls

~ is a shortcut for the current user's home directory. The default value is /home/labex In our environment.

Output:

Code  Desktop  golang  project

Copy Files

cp is a command that allows you to copy files and directories.

The following example shows how to copy the ~/.zshrc file to the ~/Desktop/zshrc-copy.

cp ~/.zshrc ~/Desktop/zshrc-copy
ls -l ~/Desktop

Output:

total 36
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-copy

Copy Directories

The following example shows how to copy the ~/Code directory to the ~/Desktop.

cp -r ~/Code ~/Desktop/Code
ls -l ~/Desktop

-r is a flag that tells the cp command to copy directories recursively.

Output:

total 36
drwxr-xr-x 2 labex labex    6 Jan 19 09:14 Code
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-copy

Copy with Details

The following example shows how to copy the ~/.zshrc file to the ~/Desktop directory with details.

cp -rv ~/.zshrc ~/Desktop/zshrc-copy-new
ls -l ~/Desktop

-r is a flag that tells the cp command to copy directories recursively.
-v is a flag that tells the cp command to be verbose.

Output:

total 44
drwxr-xr-x 2 labex labex    6 Jan 19 09:14 Code
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-copy
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-copy-new

Move Files

mv is a command that allows you to move files and directories.

The following example shows how to move the ~/Desktop/zshrc-copy file to the ~/Desktop/zshrc-move.

mv ~/Desktop/zshrc-copy ~/Desktop/zshrc-move
ls -l ~/Desktop

Output:

total 44
drwxr-xr-x 2 labex labex    6 Jan 19 09:14 Code
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-copy-new
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-move

Move Directories

The following example shows how to move the ~/Desktop/Code directory to the ~/Desktop/Code-move.

mv ~/Desktop/Code ~/Desktop/Code-move
ls -l ~/Desktop

Output:

total 44
drwxr-xr-x 2 labex labex    6 Jan 19 09:14 Code-move
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-copy-new
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-move

Move with Details

The following example shows how to move the ~/Desktop/zshrc-copy-new file to the ~/Desktop/zshrc-move-new directory with details.

mv -v ~/Desktop/zshrc-copy-new ~/Desktop/zshrc-move-new
ls -l ~/Desktop

-v is a verbose option that shows the source and destination of the move operation.

Output:

renamed '/home/labex/Desktop/zshrc-copy-new' -> '/home/labex/Desktop/zshrc-move-new'
total 44
drwxr-xr-x 2 labex labex    6 Jan 19 09:14 Code-move
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-move
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-move-new

Remove Files

rm is a command that allows you to remove(delete) files and directories.

The following example shows how to remove the ~/Desktop/zshrc-move file.

rm ~/Desktop/zshrc-move
ls -l ~/Desktop

Output:

total 36
drwxr-xr-x 2 labex labex    6 Jan 19 09:14 Code-move
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-move-new

Remove Directories

The following example shows how to remove the ~/Desktop/Code-move directory.

rm -r ~/Desktop/Code-move
ls -l ~/Desktop

-r is a flag that allows you to remove directories and their contents.

Output:

-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop
-rw-r--r-- 1 labex labex 4752 Jan 19 09:14 zshrc-move-new

Remove with Details

The following example shows how to remove the ~/Desktop/zshrc-move-new file with details.

rm -v ~/Desktop/zshrc-move-new
ls -l ~/Desktop

-v is a verbose option that shows the source and destination of the remove operation.

removed '/home/labex/Desktop/zshrc-move-new'
total 28
-rwxr-xr-x 1 labex labex  636 Jan 15 10:47 code.desktop
-rwxr-xr-x 1 labex labex  749 Jan 15 10:47 gedit.desktop
-rwxr-xr-x 1 labex labex 5106 Jan 15 10:47 gvim.desktop
-rwxr-xr-x 1 labex labex 8499 Jan 15 10:47 xfce4-terminal.desktop

Remove Without Confirmation

Sometimes you will be asked for confirmation when removing files or directories. The following example shows how to remove files and directories without confirmation.

rm -rf ~/Code
ls ~
  • The -r (recursive) option removes directories and their contents.
  • The -f (force) option ignores nonexistent files and arguments.

Output:

Desktop  golang  project

Summary

Congratulations! You have completed the basic operations lab.

In this lab, you learned six basic operations commands ls, clear, cd, cp, mv, rm.

These commands that are used frequently, so be sure to keep them in mind.

Keep exploring!

Other Linux Tutorials you may like