Open In App

Move and Copy different file types in different folders using Shell Scripts

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Shell scripts are powerful tools that allow you to automate repetitive tasks and perform complex operations on your file system. One common use case for shell scripts is moving and copying different file types to different folders. In this article, we will explore different examples of shell scripts that can be used to move and copy different file types, such as text files, video files, and document files.

 

In the above image, you can see that we have a bunch of different files in our Downloads folder with different extensions

 

And in this image, you can see that we have created different folders for different files.

1. To move files, we can use the mv command. For example, the following shell script will move all files with the “.txt” extension from the “download” folder to the “text” folder:

#!/bin/bash

# move all txt files from download to text folder
find ~/download -name '*.txt' -exec mv {} ~/text/ \;

 

Output:

 

2. To copy files, we can use the cp command. For example, the following shell script will copy all files with the “.mp4” extension from the “download” folder to the “videos” folder:

#!/bin/bash

# copy all mp4 files from download to videos folder
find ~/download -name '*.mp4' -exec cp {} ~/videos/ \;

 

Output:

 

3. To move all types of files, we can use the * wildcard, which matches all characters. For example, the following shell script will move all files from the “download” folder to the “archive” folder:

#!/bin/bash

# move all files from download to archive folder
find ~/download -name '*' -exec mv {} ~/archive/ \;

 

Output:

 

4. We can also use more complex regular expressions to match specific file types. For example, the following shell script will move all Microsoft Word documents from the “download” folder to the “docs” folder:

#!/bin/bash

# move all docx files from download to docs folder
find ~/download -regex '.*\.docx' -exec mv {} ~/docs/ \;

 

Output:

 

5. Another way to move and copy files is using rsync command. It’s a powerful tool that allows you to sync files and directories between different locations. The following shell script will copy all the files in the “download” folder to the “backup” folder:

#!/bin/bash

# copy all files from download to backup folder
rsync -avz ~/download/ ~/backup/

 

Output:

 

 

Here is the full script:

 

#!/bin/bash

# move all txt files from download to text folder
find ~/Downloads -name '*.txt' -exec mv {} ~/text/ \;
# copy all mp4 files from download to videos folder
find ~/Downloads -name '*.mp4' -exec cp {} ~/Videos/ \;
# move all files from download to archive folder
find ~/Downloads -name '*.png' -exec mv {} ~/archive/ \;
# move all docx files from download to docs folder
find ~/Downloads -regex '.*\.docx' -exec mv {} ~/docs/ \;
# copy all files from download to backup folder
rsync -avz ~/Downloads/ ~/backup/

Output:

 

As you can see in the above image we had different types of files in the Downloads folder and after running the script in the second command, we got the output and below we can see that the files are moved and copied to different folders according to the script.

Conclusion

In conclusion, shell scripts are a powerful tool that can be used to automate the process of moving and copying different file types to different folders. Whether you need to move all text files to a specific folder or copy all video files to another location, shell scripts can make the process quick and easy. With a little bit of knowledge about the command line and regular expressions, you can create powerful scripts that will save you time and effort in the long run.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads