Skip to main content

Commands used for File Handling

LS

The command “ls” displays the list of all directories, folder, and files present in the current directory.

 

LS - LTR

The above-mentioned command displays the name of directories, folders, files with their respective owner name, group’s name, and rights your user has over these.

 

 

 

ls
ls -ltr
/$ ls
bin    dev   lib    libx32      mnt   root  snap      sys  usr
boot   etc   lib32  lost+found  opt   run   srv       tim  var
cdrom  home  lib64  media       proc  sbin  swapfile  tmp
/$ ls -ltr
total 2097256
drwxr-xr-x   2 root root       4096 Feb  9  2021 mnt
drwxr-xr-x  15 root root       4096 Feb  9  2021 var
drwx------   2 root root      16384 Feb 11  2022 lost+found
lrwxrwxrwx   1 root root          8 Feb 11  2022 sbin -> usr/sbin
lrwxrwxrwx   1 root root         10 Feb 11  2022 libx32 -> usr/libx32
lrwxrwxrwx   1 root root          9 Feb 11  2022 lib64 -> usr/lib64
lrwxrwxrwx   1 root root          9 Feb 11  2022 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          7 Feb 11  2022 lib -> usr/lib
lrwxrwxrwx   1 root root          7 Feb 11  2022 bin -> usr/bin
drwxrwxr-x   2 root root       4096 Feb 11  2022 cdrom
drwx------  10 root root       4096 Feb 12  2022 tim
-rw-------   1 root root 2147487744 Aug  9  2022 swapfile
drwxr-xr-x   5 root root       4096 Aug 14  2022 home
drwxr-xr-x  14 root root       4096 Oct 22 14:08 usr
drwxrwxrwx   7 root root       4096 Nov 26 04:18 media
drwxrwxrwx  13 root root       4096 Jan  7 21:59 opt
drwxr-xr-x   2 root root       4096 Feb  5 07:19 srv
dr-xr-xr-x  13 root root          0 Feb  7 23:41 sys
dr-xr-xr-x 549 root root          0 Feb  7 23:41 proc
drwx------  12 root root       4096 Feb  8 04:25 root
drwxr-xr-x  20 root root       5120 Feb 17 23:04 dev
drwxr-xr-x  28 root root       4096 Feb 21 23:36 snap
drwxr-xr-x 168 root root      12288 Mar  1 06:24 etc
drwxr-xr-x   5 root root       4096 Mar  2 06:51 boot
drwxr-xr-x  46 root root       1500 Mar  2 20:11 run
drwxrwxrwt  32 root root      20480 Mar  2 21:06 tmp

MKDIR

The command “mkdir” allows users to create directories/folders in the system. The user running this command must have suitable rights over the parent directory to create a directory or they will receive an error.
Syntax: mkdir New_Directory’s_Name

 
mkdir NewDirectory

 

~$ mkdir poopoo
~$ 
~$ ls
 Android            Pictures
 AppImages          poopoo

RMDIR

The command “rmdir” allows users to remove directories/folders from the system. The user running this command must have suitable rights over the parent directory to remove a directory AND the directory must not have any files or sub-directories within it or you  will receive an error.
Syntax: rmdir Directory’s_Name

rmdir DirectoryName
~$ rmdir poopoo
rmdir: failed to remove 'poopoo': Directory not empty
# Could not delete directory 
# "poopoo" because it is not
# empty

~$ rm poopoo
rm: cannot remove 'poopoo': Is a directory
# Could not remove "poopoo" 
# because it is not a file

RM

The command “rm” is used to remove files from a directory.

 

 

 

 

 

 

 

 

RM -RF

Permanently deletes the specified directory and ALL files and sub-directories beneath the specified directory.

Be VERY careful using this command as you can inadvertently delete your whole drive!

rm filename

 

 

 

 

 

 

 

 

 

rm -rf /path/to/dir/name

 

# Listing shows poopoo.txt 
# file exists under 
# direcotry "poopoo"
~/poopoo$ ls
poopoo.txt

~/poopoo$ rm poopoo.txt 

# listing now shows 
# poopoo.txt has been
# removed (deleted) 
# from directory "poopoo"
~/poopoo$ ls
~/poopoo$

  

# Directory "poopoo" exists 
# in the listing below
~$ ls
 Android            Pictures
 AppImages          poopoo
 Audio              Public


~$ rm -rf poopoo
~$
# Successfully removed 
# "poopoo" directory 
# and all its contents
# as can be seen in the 
# listing below

~$ ls 
 Android            Parkitect
 AppImages          Pictures
 Audio              Public

MV

The command “mv” is used for two purposes

  • To move files or directories from one path to another path in the system.
  • To rename a file or folder.
mv Source_File_name Destination_File_Name
mv File_name New_name_for_file

CP

The command “cp” is used to copy data from a source file to the destination file. Its function is almost like the command “mv”. The only difference is by using the command “cp” the source file is not removed from the directory after its data is moved to the destination file.

cp source_file_name destination_file_name

TOUCH

Creates an empty file at the specified path with the specified name.

Useful for creating a blank file you intend to edit with a CLI editor, such as VIM or NANO.

touch /path/name/filename.ext
~$ ls doc.txt
ls: cannot access 'doc.txt': No such file or directory
~$ touch /home/tim/doc.txt
~$ ls doc.txt
doc.txt