Last Updated: November 14, 2021
·
1.599K
· mnazim

Use unix filesystem commands like cp, mv, mkdir, etc. on multiple files and directories.

Often, we need to mkdir, rm, cp, mv, touch files and directories inside multiple directories. Most often, people would resort to GUI point and click, drang and drop, copy and past, etc. but many of us are not aware of this nifty little feature in all unix filesystem related commands (by all, I mean those that I use and know about).

Talk is cheap, show me the code.

# Creating directories
$ mkdir -p assets/{img,js,sass,css}

# Moving files and directories
$ mv {img,js,sass,css} assets

# Creating files
$ touch assets/{img,js,sass,css}/.gitkeeper

# Removing directories
$ rm assets/{img,js,sass,css}/.gitkeeper

But wait, there is one rule

There must not be any spaces between the items inside the curly braces. E.g

# This is the correct way...
$ mv {img,js,sass,css} assets 

# This is not.
$ mv { img, js, sass, css } assets

# If you want files and directories with spaces in their
# names, use double quote to enclose the names.
$ mv {"i m g", "j s", "s a s s", "c s s"} assets

3 Responses
Add your response

nice tip..thanks.

over 1 year ago ·

@amaljoyc Any time :)

over 1 year ago ·

You can also easily rename a file/directory by typing

mv {oldname,newname}
over 1 year ago ·