What is file management in linux

Unix / Linux — File Management

In this chapter, we will discuss in detail about file management in Unix. All data in Unix is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

When you work with Unix, one way or another, you spend most of your time working with files. This tutorial will help you understand how to create and remove files, copy and rename them, create links to them, etc.

In Unix, there are three basic types of files −

Ordinary Files − An ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, you look at working with ordinary files.

Directories − Directories store both special and ordinary files. For users familiar with Windows or Mac OS, Unix directories are equivalent to folders.

Special Files − Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

Listing Files

To list the files and directories stored in the current directory, use the following command −

Here is the sample output of the above command −

The command ls supports the -l option which would help you to get more information about the listed files −

Here is the information about all the listed columns −

First Column − Represents the file type and the permission given on the file. Below is the description of all type of files.

Second Column − Represents the number of memory blocks taken by the file or directory.

Third Column − Represents the owner of the file. This is the Unix user who created this file.

Fourth Column − Represents the group of the owner. Every Unix user will have an associated group.

Fifth Column − Represents the file size in bytes.

Sixth Column − Represents the date and the time when this file was created or modified for the last time.

Seventh Column − Represents the file or the directory name.

In the ls -l listing example, every file line begins with a d, , or l. These characters indicate the type of the file that’s listed.

Regular file, such as an ASCII text file, binary executable, or hard link.

Block special file. Block input/output device file such as a physical hard drive.

Character special file. Raw input/output device file such as a physical hard drive.

Directory file that contains a listing of other files and directories.

Symbolic link file. Links on any regular file.

Named pipe. A mechanism for interprocess communications.

Socket used for interprocess communication.

Metacharacters

Metacharacters have a special meaning in Unix. For example, * and ? are metacharacters. We use * to match 0 or more characters, a question mark (?) matches with a single character.

Displays all the files, the names of which start with ch and end with .doc

Here, * works as meta character which matches with any character. If you want to display all the files ending with just .doc, then you can use the following command −

Hidden Files

An invisible file is one, the first character of which is the dot or the period character (.). Unix programs (including the shell) use most of these files to store configuration information.

Some common examples of the hidden files include the files −

.profile − The Bourne shell ( sh) initialization script

.kshrc − The Korn shell ( ksh) initialization script

.cshrc − The C shell ( csh) initialization script

.rhosts − The remote shell configuration file

To list the invisible files, specify the -a option to ls

Single dot (.) − This represents the current directory.

Double dot (..) − This represents the parent directory.

Creating Files

You can use the vi editor to create ordinary files on any Unix system. You simply need to give the following command −

The above command will open a file with the given filename. Now, press the key i to come into the edit mode. Once you are in the edit mode, you can start writing your content in the file as in the following program −

Once you are done with the program, follow these steps −

Press the key esc to come out of the edit mode.

Press two keys Shift + ZZ together to come out of the file completely.

You will now have a file created with filename in the current directory.

Editing Files

You can edit an existing file using the vi editor. We will discuss in short how to open an existing file −

Once the file is opened, you can come in the edit mode by pressing the key i and then you can proceed by editing the file. If you want to move here and there inside a file, then first you need to come out of the edit mode by pressing the key Esc. After this, you can use the following keys to move inside a file −

l key to move to the right side.

h key to move to the left side.

k key to move upside in the file.

j key to move downside in the file.

So using the above keys, you can position your cursor wherever you want to edit. Once you are positioned, then you can use the i key to come in the edit mode. Once you are done with the editing in your file, press Esc and finally two keys Shift + ZZ together to come out of the file completely.

Display Content of a File

You can use the cat command to see the content of a file. Following is a simple example to see the content of the above created file −

You can display the line numbers by using the -b option along with the cat command as follows −

Counting Words in a File

You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. Following is a simple example to see the information about the file created above −

Here is the detail of all the four columns −

First Column − Represents the total number of lines in the file.

Second Column − Represents the total number of words in the file.

Third Column − Represents the total number of bytes in the file. This is the actual size of the file.

Fourth Column − Represents the file name.

You can give multiple files and get information about those files at a time. Following is simple syntax −

Copying Files

To make a copy of a file use the cp command. The basic syntax of the command is −

Following is the example to create a copy of the existing file filename.

You will now find one more file copyfile in your current directory. This file will exactly be the same as the original file filename.

Renaming Files

To change the name of a file, use the mv command. Following is the basic syntax −

The following program will rename the existing file filename to newfile.

The mv command will move the existing file completely into the new file. In this case, you will find only newfile in your current directory.

Deleting Files

To delete an existing file, use the rm command. Following is the basic syntax −

Caution − A file may contain useful information. It is always recommended to be careful while using this Delete command. It is better to use the -i option along with rm command.

Following is the example which shows how to completely remove the existing file filename.

You can remove multiple files at a time with the command given below −

Standard Unix Streams

Under normal circumstances, every Unix program has three streams (files) opened for it when it starts up −

stdin − This is referred to as the standard input and the associated file descriptor is 0. This is also represented as STDIN. The Unix program will read the default input from STDIN.

stdout − This is referred to as the standard output and the associated file descriptor is 1. This is also represented as STDOUT. The Unix program will write the default output at STDOUT

stderr − This is referred to as the standard error and the associated file descriptor is 2. This is also represented as STDERR. The Unix program will write all the error messages at STDERR.

Источник

Unix and Linux directory management commands

W e use DNS (domain name system) to translate between domain names and IP addresses. For example, one can use the dig command/host command for DNS lookup on a Linux and Unix-like systems. Similarly, Linux files are referred by file names, not by inode number. So what is the purpose of a directory? You can group the files according to your usage. For example, all configuration files are stored under /etc/ directory. So the purpose of a directory is to make a connection between file names and their associated inode number. Inside every directory, you will find out two sub-directories named:

  1. . (single period) – The current directory
  2. .. (double period) – The pointer to previous directory i.e. the directory immediately above the one I am in now. The ‘ .. ‘ appears in every directory except for the root directory. The ‘ .. ‘ always points to the same inode as ‘ .

Use the ls command to list files and directories including . and .. directories on Linux or Unix:
ls -la

Find the . and .. directories on Linux or Unix-like systems using the ls command

Directory

A directory contained inside another directory is called a sub-directory. At the end the directories form a tree structure. Use the tree command to see directory tree structure:
$ tree /etc | less

Use tree command to list contents of directories in a structure like format

However according to official documentation of ext2/3 file system points that using a hashed directory index (which is under development) allows 100k-1M+ files in a single directory without performance problems. Here are my two favorite bash shell alias commands related to directory :
alias ..=’cd ..’
alias d=’ls -l | grep -E «^d»‘

Unix and Linux directory management commands

A list of standard Linux/Unix commands to work with directories and files:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Sr.No. Prefix & Description
1
Command Description Example(s)
mkdir command Creates a new directory mkdir dir1
rmdir command Deletes the specified directory if it is already empty rmdir dir1
cd command Change the current directory cd /etc/
cd .. command Go back to previous directory cd ..
pwd command Display name of current/working directory pwd
cd command cd without any parameters changes to the user’s home directory cd
mv command Copies source to target then deletes the original source mv dir1 dir2
cp command Copies source to target cp -r dir1 /path/to/dir2
rm command Removes the specified files from the file system. Directories are not removed by rm unless the option -r is used rm file1
rm -r dir1
ln command Creates an internal link from source to target ln -s /etc/hosts /tmp/link
chown command Transfers ownership of a file to the user with the specified username chown userName file
chgrp command Transfers the group ownership of a given file to the group with the specified group name chgrp dir1
chmod command Changes the access permissions chmod 0444 dir1
locate command The locate command can find in which directory a specified file is located locate file1
find command Search for a file in a given directory find $HOME -name «hello.c»
file command Detect the contents of the specified files file /etc/resolv.conf
cat command Displays the contents of a file cat data.txt
less command See the contents of the specified file less resume.txt
more command It is a filter for paging through text one screenful at a time more /etc/hosts
grep command Finds a specific search string in the specified files grep «nameserver» /etc/resolv.conf
egrep command Same as grep but extended regular expression supported egrep -i ‘err|cri|warn|’ /var/log/messages
diff command Compares the contents of any two files diff old.c new.c

I am sure all of you know the basic commands related to directories and files management. Click the link on the left column to learn more about each command and same usage.

Источник

Kali Linux – File Management

In Kali Linux, most of the operations are performed on files. And to handle these files Kali Linux has directories also known as folders which are maintained in a tree-like structure. Though, these directories are also a type of file themselves. Kali Linux has 3 basic types of files:

  1. Regular Files: It is the common file type in Linux. it includes files like – text files, images, binary files, etc. Such files can be created using the touch command. They consist of the majority of files in the Linux/UNIX system. The regular file contains ASCII or Human Readable text, executable program binaries, program data, and much more.
  2. Directories: Windows call these directories as folders. These are the files that store the list of file names and the related information. The root directory(/) is the base of the system, /home/ is the default location for user’s home directories, /bin for Essential User Binaries, /boot – Static Boot Files, etc. We could create new directories with mkdir command.
  3. Special Files: Represents a real physical device such as a printer which is used for IO operations. Device or special files are used for device Input/Output(I/O) on UNIX and Linux systems. You can see them in a file system as an ordinary directory or file.

In Unix systems, there are two types of special files for each device, i.e. character special files and block special files. For more details, read the article Unix file system.

You can read more about the Linux File Management.

Kali Linux – File Hierarchy Structure

Kali Linux follows a specific File hierarchy structure which is just a way of organizing files, filesystems, directories, installed packages, and external devices connected to the system. It basically sets a standard or a base for defining the directory structure.

In Kali Linux File Hierarchy Structure the base or the main directory which contains all the directories is the root directory depicted by a “/”.

Managing and Working with Files in Kali Linux

1. To List the Files. In Kali Linux, we use ls command to list files and directories. To use this enter the following command in the terminal.

This command will print all the file and directories in the current directory.

2. To create a new file. In Kali Linux, we use touch command to create a new file. To use this enter the following command in the terminal.

This command will create a new file named filename.extension.

3. To display the content of a file. In Kali Linux, we use cat command to display the contents of a file. To use this enter the following command in the terminal.

This command will print the content of the filename.extension file.

4. To copy files. In Kali Linux, we use cp command to copy files and directories. To use this enter the following command in the terminal.

This command will copy file form old location to new location.

5. To move files. In Kali Linux, we use mv command to move files and directories. To use this enter the following command in the terminal.

This command will move the file form old location to new location.

6. To rename a file. In Kali Linux, we use mv command to rename files and directories. To use this enter the following command in the terminal.

This command will rename the file from filename.extension to new_name.extension.

7. To delete a file. In Kali Linux, we use rm command to delete files and directories. To use this enter the following command in the terminal.

This command will remove/delete the filename.extension from the current directory.

8. To edit a file. In Kali Linux, we use nano command to edit files. To use this enter the following command in the terminal.

This command will open an editor to write to file and after completing the same press ctrl+o to save the file.

Источник

Читайте также:  Windows product policy editor
Оцените статью