Managing File From the Command Line

Managing File From the Command Line


Topic :- Managing File From the Command Line



Objectives :-

  • identify the purpose for important or files on a Linux System
  • Specify file using the relative path names.
  • Create, Copy, Move, Rename and Remove file and Directories using Command-Line utilities.

Topics

  1. The Linux File System Hierarchy
  2. Locating File by name
  3. Managing File Using Command-Line Tools

1. The Linux File System Hierarchy

Objectives

understand the fundamental file system layout, Organization, and the structure of Linux file System and the location of key file types.

All files on a Linux System are stored on file system which are organized into single inverted tree of directories, known as a file system hierarchy. this tree is inverted because the root of the tree is said to be at the top of the hierarchy, and the branches of directories and sub-directories stretch below the root directory.
The Linux File Hierarchy Structure or the Filesystem Hierarchy Standard (FHS) defines the directory structure and directory contents in Unix-like operating systems.It is maintained by the Linux Foundation.
Managing File From the Command Line

  • all files and directories appear under the root directory /, even if they are stored on different physical or virtual devices.
  • Most of these directories exist in all UNIX operating systems and are generally used in much the same way; however, the descriptions here are those used specifically for the FHS, and are not considered authoritative for platforms other than Linux.
  • the / character is also used as a directory separator in file names.
  • the Sub-directories of / are used for standardized purposes to organize files by types and purpose. this makes it easier to find files. for example, in the root directory, the sub-directory /boot is used for storing files needed to boot the System.

1.1 Important of Linux Directories

/root :-
Primary hierarchy root and root directory of the entire file system hierarchy. Home directory for the administrative super user, root.
  • Every single file and directory starts from the root directory
  • Only root user has the right to write under this directory
  • /root is root user’s home directory, which is not same as /
/bin :- 
Essential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp.
  • Contains binary executable
  • Common linux commands you need to use in single-user modes are located under this directory.
  • Commands used by all the users of the system are located here e.g. ps, ls, ping, grep, cp
/boot :-
Boot loader files, e.g., kernels, initrd.
  • Kernel initrd, vmlinux, grub files are located under /boot
  • Example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
/dev :-
Essential device files, e.g., /dev/null.
  • These include terminal devices, usb, or any device attached to the system.
  • Example: /dev/tty1, /dev/usbmon0
/etc :- 
Host-specific system-wide configuration files.
  • Contains configuration files required by all programs.
  • This also contains startup and shutdown shell scripts used to start/stop individual programs.
  • Example: /etc/resolv.conf, /etc/logrotate.conf.
/home :- 
Users’ home directories, containing saved files, personal settings, etc.
  • Home directories for all users to store their personal files.
/run :-
Runtime data for processes started since the last boot. This includes process ID files and lock files, among other things. the contents of this directory are recreated on reboot. (This directory consolidates /var/run and /var/lock from older versions of Red Hat Enterprise Linux.)   
/sbin :-
Essential system binaries, e.g., fsck, init, route.
  • Just like /bin, /sbin also contains binary executable.
  • The linux commands located under this directory are used typically by system administrator, for system maintenance purpose.
  • Example: iptables, reboot, fdisk, ifconfig, swapon
/tmp :- 
Temporary files. Often not preserved between system reboots, and may be severely size restricted.
  • Directory that contains temporary files created by system and users.
  • Files under this directory are deleted when system is rebooted.
/usr :-
installed software, shared libraries, include files, and static read-only  program data. Secondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications. important sub-directories.
  • /usr/bin : User Commands or contains binary files for user programs..
  • /usr/sbin : System administrative commands.
  • /usr/local : Locally Customized Software.
  • /usr/lib : contains libraries for /usr/bin and /usr/sbin
  • /usr/src holds the Linux kernel sources, header-files and documentation. 
/var :-
Variable data specific to this system that should persist between boots. files that dynamically change (e.g. databases, cache directories, log files, printer-spooled documents, and website content) may be found under /var.

 

2. Locating File by name

Objectives

able to correctly use absolute path names, change a working directory, and use commands to determine directory locations and contents.
the path of a file or directory specifies its unique file system location. following a file path traverses one or more named sub-directories, delimited by a forward slash( / ), until the destination is reached. Standard file behavior definition apply to directories (also called folders) the same as other file types.

2.1 Absolute Paths

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.
root@batman:~# cd /
root@batman:/# ls
0     etc             lib     lost+found  palettes  run   tmp      vmlinuz.old
bin   home            lib32   media       proc      sbin  usr
boot  initrd.img      lib64   mnt         Programs  srv   var
dev   initrd.img.old  libx32  opt         root      sys   vmlinuz
root@batman:/# ls /opt
eclipse    pycharm-community-2019.1.3
microsoft  spring-tool-suite-4-4.3.0.RELEASE-e4.12.0-linux.gtk.x86_64
root@batman:/# ls /opt/eclipse
eclipse  eclipse-inst-linux64
root@batman:/#  

Example :-
/opt/eclipse
/var/www/html
If you see all these paths started from / directory which is a root directory for every Linux/Unix machines.

2.2 Relative Paths

Like an absolute path, a relative path identifies a unique file, specifying only the path necessary to reach the file from the working directory.
Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel
root@batman:~# cd /root
root@batman:~# ls
Batman   Documents  Music     Programs  Templates  Videos
Desktop  Downloads  Pictures  Public    Thor
root@batman:~# cd Batman/
root@batman:~/Batman# ls
Java_Programs  Servers  Tutorial_Project
root@batman:~/Batman# cd Java_Programs/
root@batman:~/Batman/Java_Programs# 
If you observe there is no / before Java_Program file which indicates a relative directory to present working directory of /root/Batman.

2.3 Navigating Paths

the pwd command display the full path name of the current location, which helps determine appropriate syntax for reaching files using relative path names. the ls command list directory contents for the specified directory or, if no directory is given, for the current directory.
root@batman:~# cd Desktop/
root@batman:~/Desktop# pwd
/root/Desktop
root@batman:~/Desktop# ls
 a.out         Demo1           html          'Phython Tutorial'
 Blogger       Demo2           Java           Raptor.zip
 Books         Demo3          'Kali Linux'    today
 CppPrograms   Demo4           logo.html     'UNIT 3 Application Layer.ppt'
 CPrograms     Demo.c          OsAssignment   webdevelopments
 DBMS         'DFS programs'   OSProgram
root@batman:~/Desktop# 
in this above example we have learn about pwd and ls command.

3. Managing File Using Command-Line Tools

Objectives

able to create, copy, link, move, and remove file and subdirectory in various directories.

File managment involving creating, copying, deleting and moveing file. additionaly, directories can be created, deleted, copied, and moved to help orgnize file logically. when working at the command line, file management requires awareness of the current working directory to choose either absolute or relative path syntax as most efficient for the immediate task

Command's Syntax

1. Copy File command
    single source : cp file1 file2
   Multiple Source : cp file1 file2 file3 dir

2. Move File Command
    single source : mv file1 file2
   Multiple Source : mv file1 file2 file3 dir

3. Remove File Command
    single source : rm file1
   Multiple Source : rm -f file1 file2 file3

4. Create Directory Command
    single source : mkdir dir
   Multiple Source : mkdir -p file1/file2/dir

5. Copy Directory Command
    single source : cp -r dir1 dir2
   Multiple Source : cp -r dir1 dir2 dir3 dir4

6. Move Directory Command
    single source : mv dir1 dir2
   Multiple Source : mv dir1 dir2 dir3 dir4

7. Remove Directory
    single source : rm -r dir1
   Multiple Source : rm -rf dir1 dir2 dir3 

Examples :- 


Copy File command
// copy multiple files
root@batman:~/Desktop# cd RedHat/
root@batman:~/Desktop/RedHat# touch file1 file2 file3
root@batman:~/Desktop/RedHat# ls
file1  file2  file3
root@batman:~/Desktop/RedHat# cd ..
root@batman:~/Desktop# mkdir Redhat2
root@batman:~/Desktop/Redhat2# ls
root@batman:~/Desktop/RedHat2# cd ..
root@batman:~/Desktop/RedHat# cp file1 file2 /root/Desktop/Redhat2
root@batman:~/Desktop/RedHat# cd ..
root@batman:~/Desktop# cd Redhat2
root@batman:~/Desktop/Redhat2# ls
file1  file2
root@batman:~/Desktop/Redhat2# 

//copy single file
root@batman:~/Desktop/Redhat2# cd ..
root@batman:~/Desktop# cd RedHat/
root@batman:~/Desktop/RedHat# cp file3 /root/Desktop/Redhat2/
root@batman:~/Desktop/RedHat# cd ..
root@batman:~/Desktop# cd Redhat2/
root@batman:~/Desktop/Redhat2# ls
file1  file2  file3
root@batman:~/Desktop/Redhat2# 
in the above example the first file1 and file2 this multiple files are move from RedHat directory to Redhat2 Directory
the the single file3 are move from RedHat directory to Redhat2 directory

Move File Command 
move file command are also used to rename the file in same directory. or it used for the move file from one location to another location without changing the data contains in file.

root@batman:~# cd Desktop/
root@batman:~/Desktop# cd RedHat
// the files are present in Redhat dir
root@batman:~/Desktop/RedHat# ls
file1  file2  file3
// create a new file to perform move operation
root@batman:~/Desktop/RedHat# touch Codeworld19
root@batman:~/Desktop/RedHat# cd ..
// another dir Redhat2 the destination of file
root@batman:~/Desktop# cd Redhat2
// the codeworld19 file is not present
root@batman:~/Desktop/Redhat2# ls
file1  file2  file3
root@batman:~/Desktop/Redhat2# cd ..
root@batman:~/Desktop# cd RedHat/
//perform actual move operation 
root@batman:~/Desktop/RedHat# mv Codeworld19 /root/Desktop/Redhat2/
root@batman:~/Desktop/RedHat# cd ..
root@batman:~/Desktop# cd Redhat2/
// the codeworld19 file is now present in destination dir
root@batman:~/Desktop/Redhat2# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/Redhat2# 
root@batman:~/Desktop/Redhat2# cd ..
root@batman:~/Desktop# cd RedHat/
// after move operation is complete it absent in the source dir
root@batman:~/Desktop/RedHat# ls
file1  file2  file3
if you observe the codeworld19 file is create in the RedHat directory and then it move from RedHat to Redhat2 directory. and it remove from the source directory RedHat(after complition of move operation it not present in sourece Directory).

Remove File Command 
Remove file command are used to remove file or delet file
root@batman:~/Desktop/Redhat2# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/Redhat2# rm Codeworld19 
root@batman:~/Desktop/Redhat2# ls
file1  file2  file3
root@batman:~/Desktop/Redhat2# 

Create Directory Command
mkdir command are used to create new directory
root@batman:~/Desktop/RedHat# mkdir Codeworld19
root@batman:~/Desktop/RedHat# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/RedHat# 

Copy Directory Command
cp command are used to copy Derectory from source to destination. the one important option are used to copy directory is -r (-r means recursive). because the directory are contain file or sub-directory.

root@batman:~/Desktop/RedHat# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/RedHat# cp -r Codeworld19 /root/Desktop/Redhat2/
root@batman:~/Desktop/RedHat# cd ..
root@batman:~/Desktop# cd Redhat2/
root@batman:~/Desktop/Redhat2# ls
Codeworld19  file1  file2  file3

Move Directory Command
mv command are used to move directory from one location to another with contaning data. the one more option is requaird -r (r recursive).
root@batman:~/Desktop/Redhat2# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/Redhat2# mv Codeworld19 /root/Desktop/RedHat/
root@batman:~/Desktop/Redhat2# cd ..
root@batman:~/Desktop# cd RedHat/
root@batman:~/Desktop/RedHat# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/RedHat# cd ..
root@batman:~/Desktop# cd Redhat2/
root@batman:~/Desktop/Redhat2# ls
file1  file2  file3
root@batman:~/Desktop/Redhat2# 

Remove Directory
rm command are used to remove directory or delete a directory. the one more option is requaird -r (r recursive). -r is used because the directory are contain file or sub-directory's.
root@batman:~/Desktop/Redhat2# ls
Codeworld19  file1  file2  file3
root@batman:~/Desktop/Redhat2# rm -rvf Codeworld19/
removed directory 'Codeworld19/'
root@batman:~/Desktop/Redhat2# ls
file1  file2  file3

Referance :-
1.  Red Hat Enterprise Linux 7 RH124
     Red Hat System Administration I
     Edition 1
     Authors: Susan Lauber, Philip Sweany , Rudolf Kastl, George Hacker
     Editor: Steven Bonneville
2.  www.geeksforgeeks.org




Disclaimer :-
the above information is presented to you by codeworld19's authority on the basis of the above reference list. if you have any queries or other issues regarding this page or website, please contact to the codeworld19 authority from following contact form.
Next Post Previous Post
1 Comments
  • laxmi
    laxmi Friday, June 18, 2021

    salesforce online training from india
    Mulesoft online training from india
    Webmethods online training from india

Add Comment
comment url