[SOLVED] Transfer files whose paths or filenames contain characters prohibited by Microsoft.
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Transfer files whose paths or filenames contain characters prohibited by Microsoft.
In transferring files from a Linux computer to a windows computer, the folders and files with names or paths containing characters not allowed on windows were not transferred. There are some 700 that have illegal characters in either their file names, their paths, or possibly both. I found a way to replace all the illegal characters in all folders and files. That would enable me to copy the everything to the windows computer, skipping legal filenames, but I would prefer to create a new folder containing all of the offending folders and files with the their illegal characters replaced but preserving their paths relative to the user’s home directory. This would give me a folder containing all of the files that didn’t transfer originally, that is all the files that had illegal characters in either their path or their file name.
I want to replace the characters < > : \ / ? | { }* with an underscore.
For instance if these were all the files on the Linux computer (Here a ? could be any of the above listed prohibited characters):
/home/user/Documents/file01.txt
/home/user/Documents/folderA/file???02.txt
/home/user/Documents/folder???B/file03.txt
/home/user/Documents/Folder???C/file?04.txt
Then I would like to have a new folder with the following files on the external drive:
Documents/folderA/file___02.txt
Documents/folder___B/file03.txt
Documents/Folder___C/file_04.txt
I could then copy that file to the user’s directory in windows.
Asking a lot, but if someone is so good as to provide a solution, please explain why it works. I have found solutions to similar requests on line, but I don’t understand them, certainly not enough to modify them to suit my purpose.
I think you will have to do a little script writing to achieve your wishes. Only you have the full access to see the file names and what characters are causing the issue.
With your description it also appears that you may need to either do one directory at a time, or have the script be enabled to parse the directory tree and descend as needed to work one file at a time.
The generic flow would be similar to this for whatever language you use, though using bash is pretty simple. Note that this is an english language summary of what the program would do.
Code:
for FILE in * ; do
if "$FILE" contains <any of the unwanted characters> ; then
TMPFILE="$FILE"
while "$TMPFILE" contains <any of the unwanted characters> ; do
replace the unwanted characters in "$TMPFILE"
done
cp "$FILE" /path/to/new/folder/"$TMPFILE"
fi
done
Once all the files have been processed and the file names adjusted to your liking then the files in the new path could be moved to windows.
Personally, the very first character I would replace would be the spaces in the file names.
This is an example of one script I did to rename and move some files when I was ripping some audio books to my pc.
Code:
#!/usr/bin/sh
# rename.sh (this file name)
# this is to be called with a structure like the following
#
# for DIR in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 ; do `./rename.sh Elizabeth_Moon/Elizabeth_Moon_-_Kings_of_the_North .._Track Kings_of_the_North ".mp3" $DIR `; done
#
# where DIR is the trailing numbers of the volumes for each CD volume
# MATCH is the filename part of the original filename to be replaced
# SUB is the new filename to be assigned
# and EXT is the extension of the file being renamed.
# The order of the arguments is important
#
# It can be used for a single file or directory by judicious use of the command line
# structure but was designed to allow for easy renaming and consolidating all the track
# titles for a single book into a single directory named for the artist and book title.
if [ "x$5" = "x" ]
then
DIR=$1
SUB=$3
else
DIR=$1_$5
SUB=$3_$5
fi
MATCH=$2
EXT=$4
# `mkdir $3`
cd $DIR
for i in *$EXT
do
NAME=`basename $i $EXT`
# `echo "Original file name is $NAME"`
NEWNAME=`echo $NAME | sed s/$MATCH/$SUB/g`
# `echo "New file name is $NEWNAME"`
`mv $NAME$EXT $NEWNAME$EXT`
# `cp $NAME$EXT ../$3/$NEWNAME$EXT`
done
# cd - >/dev/null 2>&1
cd -
I found a way to replace all the illegal characters in all folders and files.
To me, what you want reduces to copy all files containing the illegal characters to a new directory (folder), so they can be handled separately.
Code:
#!/bin/bash
# Create the new directory
target="/tmp"
mkdir "$target/Documents"
# Set a shell option to descend into subdirectories
shopt -s globstar
# Change to top directory
cd $HOME/Documents
# Loop through files and copy if the path and/or name contains any of :?|{}*<>\
for f in **/*; do
## Some characters need to be escaped with a preceding backslash in the bracket expression
[[ "$f" =~ [:?|{}*\<\>\\]+ ]] && cp -a "$f" "$target/Documents/$f"
done
NB - I have not included the / character as it is the Linux delimiter between path and filename.
I think computersavy's suggested outline to solve my problem will copy all files to one directory, but will not include the paths. I need to have all files in a file structure that replicates the original file structure, so that the modified files will find their way into their original folders when I copy them to the new computer.
I think allend's suggestion does what I want it to, but it seemed to choke for a reason that I cannot understand. I created a test folder containing several sample files. It copied most of them and put them in the folders where they belong, but for some reason it didn't copy all the folders. here are the source and target directories:
It worked better with cp -a. With cp -p I got only the Documents folder with just one text file contained in the Documents folder. No subfolders.
Here the error messages with cp -p:
rcdawson@PantherBox:~/Scripts$ ./RemoveIllegalFileNames4WindowsTest.sh
cp: -r not specified; omitting directory 'Folder1WithIILegal:?*NameLevel1'
cp: cannot create regular file '/tmp/Documents/Folder1WithIILegal:?*NameLevel1/FileWithIllegalName*:?inFolderWithLegalName.txt': No such file or directory
cp: cannot create regular file '/tmp/Documents/Folder1WithIILegal:?*NameLevel1/FileWithLegalNameinFolderWithILLegalName.txt': No such file or directory
cp: -r not specified; omitting directory 'Folder1WithIILegal:?*NameLevel1/SubfolderWithLegalNameInSubfolderWithIllegalName'
cp: cannot create regular file '/tmp/Documents/Folder1WithIILegal:?*NameLevel1/SubfolderWithLegalNameInSubfolderWithIllegalName/FileWithIllegalName*:?inFolderWithIllegalName.txt': No such file or directory
cp: cannot create regular file '/tmp/Documents/Folder1WithIILegal:?*NameLevel1/SubfolderWithLegalNameInSubfolderWithIllegalName/FileWithLegalNameinFolderWithIlLegalName': No such file or directory
Since it complained that -r wasn't specified, I changed to cp -r. This got me pretty much back to where I was with cp -a. It copied more, but not all. Here is the error message.
cp: cannot create regular file '/tmp/Documents/FolderWithLegalNameLevel1/FileWithIllegalName*:?inFolderWithLegalName.txt': No such file or directory
cd /home/user && pax -r -w -s'/[:?|\\{}]/_/g' Documents /path/to/destfolder
(The destfolder must exist.)
Unfortunately pax is broken, at least in SUSE Linux: it omits the leading characters before a match, and the /g option does not (or cannot) apply.
EDIT:
Just read it on the Internet:
03.08.2017 — In SLE 12 SP1, pax was replaced with spax (from the package star ).
So the spax is broken.
And it works well in SLE 11 SP4 that has got a Posix-compliant pax.
Last edited by MadeInGermany; 03-10-2022 at 06:51 AM.
I wrote a script to download files from a specific website. I manually populate a list then it does the rest at certain hours of the night. I wanted it all in plain english with no punctuation save the .mp4/ .mp3 . The script ended up with a nasty looking sed line. Could probably be better. This is how I accomplished the same type of thing only for my own sanity in keeping a single alphabet. Just filters through all the combos on the download site and replaces with it's appropriate english counterpart / or a space.
Solved! Transfer files whose paths or filenames contain characters prohibited by Microsoft
At first the pax command stalled, but one, or maybe two, corrections and it works like a charm:
After poring over the pax man page and the examples there, I found that the command needed a "p" after the "g" in order to recognize that there was a pattern involved, and maybe that it needs a space after the "s". The samples had a space after "s", so I incorporated that as well. Putting in the space without the "g" didn't solve the problem.
This moved everything in the test directory without a hitch!
pax -rw . -s '/[:?|\\{}]/_/gp' TargetDirectory
Thank you. Thanks to all who took the time to offer suggestions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.