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
Creating Directory with mkdir command
The mkdir command creates an new director.
mkdir new_directory
If you want to create parent directory as well then use this command. Flag -p is required.
mkdir -p parent_directory/child_directory
2. Copying Filea and Directorys
For copying file from one location to another location we used cp command.
cp file.txt /path/to/destination/
Also, you can copy multiple files.
cp file1.txt file2.txt /path/to/destination/
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/
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/
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
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
Deleting Directories using rm -r
As you know -r stands for recursive, So we can delete fileas and content recursively.
rm -r directory_name
For deleting forcefully any directory or files we can below command
rm -rf directory_name
5. Checking file information
Listing files
The ls command list the files and directory of the current directory.
ls
For more details like permission, size or date we can use below command
ls -l
File size checking using du
The du command estimates files and directory memory usage.
du -h filename.txt
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
Changing ownership with chown
The chown command is useful for changing ownership.
chown user:group filename.txt
Top comments (0)