Still have one question...

Story: Taming the tar command: Tips for managing backups in LinuxTotal Replies: 6
Author Content

Sep 19, 2020
2:38 PM EDT
How to handle files with names in their blanks correctly with GNU tar?

Lots of music files and directories are that way. For example, on my server my mp3 directory has a sub-directory David Garrett/.

The command "cp -r mp3 /foo/" works correctly -- I get, among other things /foo/mp3/David Garrett/

But when I tar the mp3 directory and then un-tar it under /foo, I get names with quotes in them, e.g., /foo/mp3/'David Garret'/. "scp" does the same thing -- the command "scp -r mp3 someone@somehost/foo/" creates somehost/foo/'David Garret'/ -- again with quotes.

How do I fix this (mis-)behavior?

Sep 19, 2020
7:13 PM EDT
Who's crazy idea was it anyway to start using the space character for both an element separator AND a name component? That bad decision has cost humanity lifetimes of wasted effort (not just for Linux users)

Personally whenever I get a file with spaces I immediately replace spaces with underscores. I find that to be a time saving step in the long run.

Sep 20, 2020
4:18 AM EDT
> I get names with quotes

It seems that bash adds the quotes in stdout only, they are not stored in the file names. A GUI file manager does not show any quotes in the file names. tar -tf foo.tar neither.

>lifetimes of wasted effort...replace spaces with underscores

Yep, right or wrong, I agree, and do same.


Sep 21, 2020
7:36 PM EDT
Seen elsewhere, posted by someone who's not me:

    for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
...but I happily snarfed it to disk and called it ".space2bar" (dotted name so it doesn't show up in an Apache index of the subdir where I use it).

Sep 21, 2020
7:52 PM EDT
Another possibility, in Bash:

$ for file in * ; do mv "$file" ${file/ /_} ; done
Should also work in ksh. IIRC, ksh had it first.

Sep 22, 2020
6:45 AM EDT
>for file in * ; do mv "$file" ${file/ /_} ; done

This would replace the first space only.

for file in * ; do mv "$file" ${file// /_} ; done

replaces all spaces;


Sep 22, 2020
5:09 PM EDT
Woops, yup, you're right.

"Many eyes", indeed.

Posting in this forum is limited to members of the group: [ForumMods, SITEADMINS, MEMBERS.]

Becoming a member of LXer is easy and free. Join Us!