Latest Hacking,Hacker News for Hackers and Hacking and Secuirty Community

Latest Hacking News for Hacker News for Security Guys

Managing Files From The Command Line

6 min read

Linux File System Hierarchy Concepts

All files of the  Linux Operating System, stored on file systems

A single inverted tree of directories, known as a file-system hierarchy

Inverted means , the root of the tree is at the top of the hierarchy

And the branches of directories and subdirectories, below the root.

The / directory is the root directory at the top of the file-system hierarchy.

The / character is also used as a directory separator in file names.

Red Hat Enterprise Linux Directories

/USR:

  • Installed software,
  • Shared libraries
  • Include  files
  • Program   

/USR/BIN:

 User commands

/USR/SBIN:

System administration commands

/USR/LOCAL:

Locally customized software

/ETC  

Configuration Files

 /VAR

Variable data

Log files

/RUN

Runtime data

Process ID files

Lock files

/HOME        

Regular Users database

Called home directory of Normal User

Configuration files

/ROOT                      

Home directory of the Superuser (root)

/TMP

Space to store data temporary

If not accessed, changed, or modified within 10 days, data will be delete automatically

/VAR/TMP

In this location, if not access, change, or modify within 30 days, data will be are deleted automatically.

/BOOT          

In this all the booting files contain and kernel to boot the system

/DEV

Contains special device files like hard disk, pen drive DVD etc.

Absolute Paths And Relative Paths

The path of a file or directory specifies its unique file system location.

Absolute Paths

Begins  with the  (/)  sign as the first character is an absolute path

Relative Paths

Start from the working directory  

OR current working directory

Not start with forward slash (/)as the first character in relative path

The use of PWD command in Linux

The pwd command is used to display the full path name of the current working directory

[[email protected] ~]# pwd
/root

[[email protected] d3]# pwd
/d1/d2/d3

The use of LS Command

The lS command lists directory contents for the specified directory

If no directory is given, show the content for the current working directory.

Linux File System Hierarchy Concepts

ls means short list

[[email protected] ~]# ls

ls -l means long list, show file type, permissions, user ownership, group ownership, size, time stamp and files and the folder name

[[email protected] ~]# ls -l

ls -la means, show all (hidden and unhidden) files and folders

[[email protected] ~]# ls -la

ls -lh means, show files and folder size in human readable format like KB, MB, GB

[[email protected] ~]# ls -lh

ls -ld means, list the permissions of individual directory

[[email protected] ~]# ls -ld craw_data/
drwxrwx—. 2 root craw_salesgroup 6 Oct 14 21:10 craw_data/

ls -li means, show the inode number of any file and folder

[[email protected] ~]# ls -ldi craw_data/
101686854 drwxrwx—. 2 root craw_salesgroup 6 Oct 14 21:10 craw_data/

ls -lR means, show the sub-directories in recursive manner

[[email protected] ~]# ls -lR /data/IT

/data/IT/:
drwxr-xr-x. 2 root root 6 Oct 14 21:23 accounts
drwxr-xr-x. 2 root root 6 Oct 14 21:23 marketings
drwxr-xr-x. 2 root root 6 Oct 14 21:23 others
drwxr-xr-x. 2 root root 6 Oct 14 21:23 sales

ls –lt means, short the files and folders according to time, newest first

[[email protected] ~]# ls -lt
total 8
drwxrwx—. 2 root craw_salesgroup 6 Oct 14 21:10 craw_data

ls -lZ means, show the SElinux security context of any file and directory

[[email protected] ~]# ls -ldZ craw_data/
drwxrwx—. 2 root craw_salesgroup unconfined_u:object_r:admin_home_t:s0 6 Oct 14 21:10 craw_data/

The use of CD command in linux

The CD command is used to change your shell’s current working directory.

If, not specify any arguments with the command, it will change to your home directory.

 The  tilde character (~) when your current working directory is your home directory.

CD Usage with Example:

cd means change directory, if we use the cd without any argument, move to home directory of any User

cd .. means, to exit the directory one by one, move to Parent Directory

Note:

Single Dot(.) means Present Working Directory

Double Dot (..) means Parent Directory

cd – means, move to previous location

[[email protected] ~]# cd /data/IT/sales/
[[email protected] sales]# ls
[[email protected] sales]# cd
[[email protected] ~]# cd –
/data/IT/sales

cd <Directory Name> means, move to that directory

[[email protected] sales]# cd /data/
[[email protected] data]#

[[email protected] sales]#

The use of TOUCH Command

The touch command is used to update a file’s timestamp according the current date and time

Create empty files (Zero Byte in size)

The use of LS Command

LS command is used to list the files and folders

Has multiple options like:

-l                                                show long listing format

-a                                             show all files, including hidden files

-R –recursive                     include the contents of all subdirectories

-d,  –directory                   list directories themselves, not their contents

-h, –human-readable     with -l and -s, print sizes

-i, –inode   –                          print the index number of each file

-r, –reverse                       reverse order while sorting

The use of CD command

CD means Change Directory 

used to move from one directory to another directory

CD options:

CD                               Home Directory of any User

CD                               Move on Previous Directory

CD ..                           Exit Directory one by one or move on parent directory

CD ../../../                 Exit one or more directory at a time

. (Dot)                        Present Working Directory

.. (Dot Dot)              Parent Directory

~ (Curl)                      Home directory

The use of MKDIR Command

Means  Make Directory

Used to create one or more directories or subdirectories

Takes as arguments a list of paths to the directories to create the directory

Options:

-p, –parents            Make parent directories  as needed

-v, –verbose           print a message for each created directory

The use of CP Command

The cp command is used to copies a file

Creating a new file either in the current directory or in a specified directory.

Copy  multiple files in the directory.

If the destination file already exists, overwrites the file

The  -r (recursive),  to copy the directory and its contents

The use of MV Command

The mv command is used to moves files from one location to another location

Moving a file is effectively the same as renaming a file

File contents remain unchanged

Used to rename a file

Used to move a file to a different directory

The use of  RM Command

The rm command is used to removes files

The rm -r or –recursive, used remove directories

Have no command-line undelete feature to restore data

The rm -ri command to interactively prompt for confirmation before deleting

The -f option takes priority and will not be prompted for confirmation

The use of RMDIR Command

The rmdir command is used to remove the directory that is empty

The rm -d (which is equivalent to rmdir), or rm -r.

Making Links Between Files

Can create multiple names that point to the same file

Has two ways to do this by creating a hard link to the file, or by creating a soft link (sometimes called a symbolic link) to the file

Terms related to Hard Links

Every file starts with a single hard link

Create a new hard link to a file, create another name that points to that same data

The new hard link acts exactly like the original file name

The  ln command to create a new hard link (another name) that points to an existing file.

Needs at least two arguments, a path to the existing file, and the path to the

hard link that you want to create.

Use the –I option with the ls command to list the files’ inode number.

If the files are on the same file system (discussed in a moment) and their inode numbers are the same,

The  files are hard links pointing to the same data.

The hard links that reference the same file will have the same link count, access permissions, user and group ownerships, time stamps, and file content.

If we do any change  in one hard link, all other hard links, will show the new information as well

Data is only deleted from storage when the last hard link is deleted.

Limitations of Hard Links

The hard links can only be used with regular files not with the directory

Hard links can only be used if both files are on the same file system

The file-system hierarchy can be made up of multiple storage devices

Create Soft Links

The ln -s command is used to create a soft link, called a symbolic link

A soft link is not a regular file

It is a special type of file that points to an existing file or directory

Can create a soft link on different file systems

Can point to a directory or special file, not just a regular file

A soft link pointing to a missing file is called a dangling soft lin

One side-effect of the dangling soft link you later

create a new file with the same name as the deleted file the soft link will no longer be “dangling” and will point to the new file

Hard links do not work like this

Leave a Reply

Your email address will not be published. Required fields are marked *