Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have checked the man pages but I am still having issues understanding them. I have a directory on my server which contains files that have spaces in the names ex. This Is How It Looks.mp3
Can I use the find command to remove the spaces and replace it with _ underscore? If not, which command can I use to complete this task?
jailshell-3.2$ for i in * do mv -n "$i" $(echo "$i" | tr [:space:] _);
>
>
I did it in different ways and I just keep getting that > simple which means I am doing something wrong. Do I have to run this as a script or I'm sure that it's that I am messing up
H_TeXMeX_H made a mistake. It should say "done" at the end of that command (to mark the end of the loop). The ">" prompt means that your command isn't yet finished (use Ctrl+C to cancel it).
Also, remember that it will only work in the current directory. It will not go into subdirectories like find would.
rename would also work:
rename -n 's/ /_/g' *
-n is a dry run, to test if one is happy. If all files end with mp3, *mp3 is probably a better wildchard.
The man-page (on Debian) is rather short.
jailshell-3.2$ for i in * do mv -n "$i" $(echo "$i" | tr [:space:] _);
>
>
I did it in different ways and I just keep getting that > simple which means I am doing something wrong. Do I have to run this as a script or I'm sure that it's that I am messing up
Hi,
I do not see any syntactical mistakes in H_TexMex_H's solution. However, if you want to make a one liner then you will have to put a semicolon before the 'do'. You also forgot the trailing done, which also needs t be preceeded by a ';'.
Here is a similiar, bash only solution:
Code:
for f in *;do echo mv -n "$f" "${f// /_}";done
If the results look ok, then remove the 'echo' to make the changes actually happen.
Hope this helps.
YES!!! The above command worked! It removed the blanks and replaced them with the underscore just like I wanted! Just ask requested, here is the output anyway.
Quote:
-jailshell-3.2$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:
none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups
Report bugs to <bug-coreutils@gnu.org>.
Thank you everyone for all of your help!! Have a great weekend!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.