- Unix / Linux — File Management
- Listing Files
- Metacharacters
- Hidden Files
- Creating Files
- Editing Files
- Display Content of a File
- Counting Words in a File
- Copying Files
- Renaming Files
- Deleting Files
- Standard Unix Streams
- Unix and Linux directory management commands
- Directory
- Unix and Linux directory management commands
- Kali Linux – File Management
- Kali Linux – File Hierarchy Structure
- Managing and Working with Files in Kali 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.
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:
- 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.
- 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.
- 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.
Источник