How to create and remove multiple files and directories using one command in Linux

How to create and remove multiple files and directories using one command in Linux

Tired of writing the same command multiple times with just different files or directory names? here I am going to show quick tips on how you can do that in just one command.

1. Create multiple files

command: touch {file1,file2,file3}

you can use this command and enter the name of multiple files in the bracket. but there is another way as well

command: touch file{1..10}

Here you can see you just have to write the number how many files you want and also you can change the name you want.

2. remove multiple files

command: rm file{11..20}

user rm command to remove files with the same syntax we used for creating files.

3. Create multiple directories

same as the file command but for the directory, we use mkdir

command: mkdir dirname{1..5}

Another way is by putting the file name in brackets

command: mkdir {ubuntu, centos, kali}

4. Remove multiple directories

command: rmdir {ubuntu, centos, kali}

But how to create multiple directories inside a new directory?

5. How to create multiple directories inside a directory.

let’s try by writing command: mkdir /data/{ubuntu,centos,kali}

It is not working and why? because we don’t have a data directory in our Documents directory. It is trying to create all three directories inside the data directory.

To make this command work we need to modify the command.

command: mkdir -p /data/{ubuntu,centos,kali}

Still, now working? you got the point now about why it is not working, yes we need to run this command as sudo.

command: sudo mkdir -p /data/{ubuntu,centos,kali}

Finally, it is working and we created multiple directories inside the directory.

I hope you found this interesting and insightful.

Thank you