DEV Community

Cover image for File Management in Bash : Commands and Examples
Krunal Kanojiya
Krunal Kanojiya

Posted on • Originally published at techalgospotlight.com

File Management in Bash : Commands and Examples

File management is one of the most common and fundamental task in everyday life. In this tutorial we will see how actually we can manage files using bash terminal.

Using Bash we can do so many things, Example, We can organise files, automating file operations, permissions management and processing in directories.

Let's see some of the common things like creating, copying , moving and deleting files in directory.


1. Creating Files and Directory

Creating Files with Touch

Touch command is used for creating an empty file or updating existing timestamps of file.

touch filename.txt
Enter fullscreen mode Exit fullscreen mode

Creating Directory with mkdir command

The mkdir command creates an new director.

mkdir new_directory
Enter fullscreen mode Exit fullscreen mode

If you want to create parent directory as well then use this command. Flag -p is required.

mkdir -p parent_directory/child_directory
Enter fullscreen mode Exit fullscreen mode

2. Copying Filea and Directorys

For copying file from one location to another location we used cp command.

cp file.txt /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

Also, you can copy multiple files.

cp file1.txt file2.txt /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

Copying Directories with cp -r command

The -r stands for recursive, It is very helpful for coping directories with their content.

cp -r source_directory /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

3. Moving and Renaming files

Moving files with mv

The mv command is very useful for moving files from one directory to another. Also we can rename the directory with this command.

mv file.txt /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

Renaming Files

As you know we can rename file using mv command but we have to specify new name at the end.

mv old_filename.txt new_filename.txt
Enter fullscreen mode Exit fullscreen mode

4. Deleting Files and Directories

Deleting files with rm

rm command used for delete the file or files. (Caution - If we removed files using rm, Then it is very hard to recover)

rm filename.txt
Enter fullscreen mode Exit fullscreen mode

Deleting Directories using rm -r

As you know -r stands for recursive, So we can delete fileas and content recursively.

rm -r directory_name
Enter fullscreen mode Exit fullscreen mode

For deleting forcefully any directory or files we can below command

rm -rf directory_name
Enter fullscreen mode Exit fullscreen mode

5. Checking file information

Listing files

The ls command list the files and directory of the current directory.

ls
Enter fullscreen mode Exit fullscreen mode

For more details like permission, size or date we can use below command

ls -l
Enter fullscreen mode Exit fullscreen mode

File size checking using du

The du command estimates files and directory memory usage.

du -h filename.txt
Enter fullscreen mode Exit fullscreen mode

6. Changing file permissions and ownership

Modify permission using chmod

chmod command can change the file permissions. Example: Giving read and write permission to owner and read permission for rest of the user.

chmod 644 filename.txt
Enter fullscreen mode Exit fullscreen mode

Changing ownership with chown

The chown command is useful for changing ownership.

chown user:group filename.txt
Enter fullscreen mode Exit fullscreen mode

Top comments (0)