Loop over files with spaces in their names, create new folder from first character in each file and move...
I'm attempting to understand my 401k match and how much I need to contribute to maximize the match
What happens when the drag force exceeds the weight of an object falling into earth?
Why doesn't Dany protect her dragons better?
Names of the Six Tastes
logo selection for poster presentation
Why is there a cap on 401k contributions?
Light Switch Neutrals: Bundle all together?
"I can't place her": How do Russian speakers express this idea colloquially?
What's an appropriate age to involve kids in life changing decisions?
How long can fsck take on a 30 TB volume?
How is it believable that Euron could so easily pull off this ambush?
99 coins into the sacks
Why doesn't increasing the temperature of something like wood or paper set them on fire?
Are there vaccine ingredients which may not be disclosed ("hidden", "trade secret", or similar)?
Can the president of the United States be guilty of insider trading?
How can I test a shell script in a "safe environment" to avoid harm to my computer?
What should I use to get rid of some kind of weed in my onions
Whose birthyears are canonically established in the MCU?
How to adjust Venn Diagram for A^c and A - B
How would an instant or sorcery with an effect that targets work with Feather?
The unknown and unexplained in science fiction
mini sub panel?
Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?
Identity of a supposed anonymous referee revealed through "Description" of the report
Loop over files with spaces in their names, create new folder from first character in each file and move files to folders
Why *not* parse `ls` (and what do to instead)?Create a new link to access all files and foldersMove files to multiple foldersgrep two searched wordscannot move Directory not emptyCannot mv to a subdirectory of itself7zip and Move Files with Spaces in Bash ScriptOptimize find -exec {} with multiple conditions : specific files in a dir and specific subdirectories in this dirCreating text file with content within a mkdir listRead from a file and return new line with loopCreate files inside folders with spaces in name with bash script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm trying to execute this bash script that loops over files (that have spaces in the name) in the current directory and creates a new folder with the first character of the file (if not already created) and moves that file to the folder. This is my code:
for i in `/bin/ls | xargs`
do
dir=`echo "$i" | cut -c 1 -`
mkdir -m777 -p "$dir"
mv "$i" "$dir"
done
The problem with this seems to be that it treats the each word in the file as a separate file, so although it creates the folder correctly , it can't move the file to that folder because the name of the file that it looks for is only the first word of the actual file. I looked at other answers to similar questions but this is the closest I've been able to get.
EDIT:
I replaced " for i in /bin/ls | xargs
" with " for i in * " as @steeldriver suggested, and although it fixed my original problem, I'm getting errors like these:
mv: cannot move '`' to a subdirectory of itself, '`/`'
mv: invalid option -- ' '
mv: missing destination file operand after '-'
mv: invalid option -- '.'
mv: invalid option -- ')'
mv: invalid option -- '+'
mv: cannot move ''$'340' to a subdirectory of itself, ''$'340''/'$'340'
mv: cannot move ''$'303' to a subdirectory of itself, ''$'303''/'$'303'
mv: cannot move ''$'305' to a subdirectory of itself, ''$'305''/'$'305'
mv: invalid option -- '1'
I think some of these files may start with non-ascii characters (I can't view the contents because there are too many files). Is there a work around to handle these cases?
shell-script
New contributor
add a comment |
I'm trying to execute this bash script that loops over files (that have spaces in the name) in the current directory and creates a new folder with the first character of the file (if not already created) and moves that file to the folder. This is my code:
for i in `/bin/ls | xargs`
do
dir=`echo "$i" | cut -c 1 -`
mkdir -m777 -p "$dir"
mv "$i" "$dir"
done
The problem with this seems to be that it treats the each word in the file as a separate file, so although it creates the folder correctly , it can't move the file to that folder because the name of the file that it looks for is only the first word of the actual file. I looked at other answers to similar questions but this is the closest I've been able to get.
EDIT:
I replaced " for i in /bin/ls | xargs
" with " for i in * " as @steeldriver suggested, and although it fixed my original problem, I'm getting errors like these:
mv: cannot move '`' to a subdirectory of itself, '`/`'
mv: invalid option -- ' '
mv: missing destination file operand after '-'
mv: invalid option -- '.'
mv: invalid option -- ')'
mv: invalid option -- '+'
mv: cannot move ''$'340' to a subdirectory of itself, ''$'340''/'$'340'
mv: cannot move ''$'303' to a subdirectory of itself, ''$'303''/'$'303'
mv: cannot move ''$'305' to a subdirectory of itself, ''$'305''/'$'305'
mv: invalid option -- '1'
I think some of these files may start with non-ascii characters (I can't view the contents because there are too many files). Is there a work around to handle these cases?
shell-script
New contributor
1
for i in *; do ...
- see Bash Pitfall #1
– steeldriver
4 hours ago
Welcome to Unix & Linux! It is generally a really bad idea to parse the output ofls
. You should probably look into either usingfind
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here.
– DopeGhoti
4 hours ago
Also, this will fail if you already (for example) have a file calledt
.
– DopeGhoti
4 hours ago
add a comment |
I'm trying to execute this bash script that loops over files (that have spaces in the name) in the current directory and creates a new folder with the first character of the file (if not already created) and moves that file to the folder. This is my code:
for i in `/bin/ls | xargs`
do
dir=`echo "$i" | cut -c 1 -`
mkdir -m777 -p "$dir"
mv "$i" "$dir"
done
The problem with this seems to be that it treats the each word in the file as a separate file, so although it creates the folder correctly , it can't move the file to that folder because the name of the file that it looks for is only the first word of the actual file. I looked at other answers to similar questions but this is the closest I've been able to get.
EDIT:
I replaced " for i in /bin/ls | xargs
" with " for i in * " as @steeldriver suggested, and although it fixed my original problem, I'm getting errors like these:
mv: cannot move '`' to a subdirectory of itself, '`/`'
mv: invalid option -- ' '
mv: missing destination file operand after '-'
mv: invalid option -- '.'
mv: invalid option -- ')'
mv: invalid option -- '+'
mv: cannot move ''$'340' to a subdirectory of itself, ''$'340''/'$'340'
mv: cannot move ''$'303' to a subdirectory of itself, ''$'303''/'$'303'
mv: cannot move ''$'305' to a subdirectory of itself, ''$'305''/'$'305'
mv: invalid option -- '1'
I think some of these files may start with non-ascii characters (I can't view the contents because there are too many files). Is there a work around to handle these cases?
shell-script
New contributor
I'm trying to execute this bash script that loops over files (that have spaces in the name) in the current directory and creates a new folder with the first character of the file (if not already created) and moves that file to the folder. This is my code:
for i in `/bin/ls | xargs`
do
dir=`echo "$i" | cut -c 1 -`
mkdir -m777 -p "$dir"
mv "$i" "$dir"
done
The problem with this seems to be that it treats the each word in the file as a separate file, so although it creates the folder correctly , it can't move the file to that folder because the name of the file that it looks for is only the first word of the actual file. I looked at other answers to similar questions but this is the closest I've been able to get.
EDIT:
I replaced " for i in /bin/ls | xargs
" with " for i in * " as @steeldriver suggested, and although it fixed my original problem, I'm getting errors like these:
mv: cannot move '`' to a subdirectory of itself, '`/`'
mv: invalid option -- ' '
mv: missing destination file operand after '-'
mv: invalid option -- '.'
mv: invalid option -- ')'
mv: invalid option -- '+'
mv: cannot move ''$'340' to a subdirectory of itself, ''$'340''/'$'340'
mv: cannot move ''$'303' to a subdirectory of itself, ''$'303''/'$'303'
mv: cannot move ''$'305' to a subdirectory of itself, ''$'305''/'$'305'
mv: invalid option -- '1'
I think some of these files may start with non-ascii characters (I can't view the contents because there are too many files). Is there a work around to handle these cases?
shell-script
shell-script
New contributor
New contributor
edited 3 hours ago
Logan
New contributor
asked 4 hours ago
LoganLogan
113
113
New contributor
New contributor
1
for i in *; do ...
- see Bash Pitfall #1
– steeldriver
4 hours ago
Welcome to Unix & Linux! It is generally a really bad idea to parse the output ofls
. You should probably look into either usingfind
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here.
– DopeGhoti
4 hours ago
Also, this will fail if you already (for example) have a file calledt
.
– DopeGhoti
4 hours ago
add a comment |
1
for i in *; do ...
- see Bash Pitfall #1
– steeldriver
4 hours ago
Welcome to Unix & Linux! It is generally a really bad idea to parse the output ofls
. You should probably look into either usingfind
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here.
– DopeGhoti
4 hours ago
Also, this will fail if you already (for example) have a file calledt
.
– DopeGhoti
4 hours ago
1
1
for i in *; do ...
- see Bash Pitfall #1– steeldriver
4 hours ago
for i in *; do ...
- see Bash Pitfall #1– steeldriver
4 hours ago
Welcome to Unix & Linux! It is generally a really bad idea to parse the output of
ls
. You should probably look into either using find
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here.– DopeGhoti
4 hours ago
Welcome to Unix & Linux! It is generally a really bad idea to parse the output of
ls
. You should probably look into either using find
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here.– DopeGhoti
4 hours ago
Also, this will fail if you already (for example) have a file called
t
.– DopeGhoti
4 hours ago
Also, this will fail if you already (for example) have a file called
t
.– DopeGhoti
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
To loop over files with spaces in their names, the shell is plenty, no need to call ls
:
for i in * # * replaces the complex (and unquoted) `/bin/ls | xargs`
do
dir=${i%"${i#?}"} # replaces the slow subshell `echo "$i" | cut -c 1 -`
echo "$i" # just to show that an * is enough (and accepts spaces).
done
And to process each file listed (which include directories) you should check that the filename is a file (not a directory) and also check if the directory doesn't exist before creating it.
for i in *
do
if [ -f "$i" ]; then
dir=${i%"${i#?}"}
if [ ! -d "$dir" ]; then
mkdir -m777 -p "$dir"
fi
mv "$i" "$dir"
if [ "$?" -ne 0 ]; then
echo "An error occurred moving file "$i" to dir "$dir""
fi
fi
done
add a comment |
With GNU Parallel it looks like this:
parallel 'mkdir -p {=s/(.).*/$1/=}; mv {} {=s/(.).*/$1/=}' ::: *
(Edit: Just noted you are asking for files - not dirs. / is removed).
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Logan is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f517665%2floop-over-files-with-spaces-in-their-names-create-new-folder-from-first-charact%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To loop over files with spaces in their names, the shell is plenty, no need to call ls
:
for i in * # * replaces the complex (and unquoted) `/bin/ls | xargs`
do
dir=${i%"${i#?}"} # replaces the slow subshell `echo "$i" | cut -c 1 -`
echo "$i" # just to show that an * is enough (and accepts spaces).
done
And to process each file listed (which include directories) you should check that the filename is a file (not a directory) and also check if the directory doesn't exist before creating it.
for i in *
do
if [ -f "$i" ]; then
dir=${i%"${i#?}"}
if [ ! -d "$dir" ]; then
mkdir -m777 -p "$dir"
fi
mv "$i" "$dir"
if [ "$?" -ne 0 ]; then
echo "An error occurred moving file "$i" to dir "$dir""
fi
fi
done
add a comment |
To loop over files with spaces in their names, the shell is plenty, no need to call ls
:
for i in * # * replaces the complex (and unquoted) `/bin/ls | xargs`
do
dir=${i%"${i#?}"} # replaces the slow subshell `echo "$i" | cut -c 1 -`
echo "$i" # just to show that an * is enough (and accepts spaces).
done
And to process each file listed (which include directories) you should check that the filename is a file (not a directory) and also check if the directory doesn't exist before creating it.
for i in *
do
if [ -f "$i" ]; then
dir=${i%"${i#?}"}
if [ ! -d "$dir" ]; then
mkdir -m777 -p "$dir"
fi
mv "$i" "$dir"
if [ "$?" -ne 0 ]; then
echo "An error occurred moving file "$i" to dir "$dir""
fi
fi
done
add a comment |
To loop over files with spaces in their names, the shell is plenty, no need to call ls
:
for i in * # * replaces the complex (and unquoted) `/bin/ls | xargs`
do
dir=${i%"${i#?}"} # replaces the slow subshell `echo "$i" | cut -c 1 -`
echo "$i" # just to show that an * is enough (and accepts spaces).
done
And to process each file listed (which include directories) you should check that the filename is a file (not a directory) and also check if the directory doesn't exist before creating it.
for i in *
do
if [ -f "$i" ]; then
dir=${i%"${i#?}"}
if [ ! -d "$dir" ]; then
mkdir -m777 -p "$dir"
fi
mv "$i" "$dir"
if [ "$?" -ne 0 ]; then
echo "An error occurred moving file "$i" to dir "$dir""
fi
fi
done
To loop over files with spaces in their names, the shell is plenty, no need to call ls
:
for i in * # * replaces the complex (and unquoted) `/bin/ls | xargs`
do
dir=${i%"${i#?}"} # replaces the slow subshell `echo "$i" | cut -c 1 -`
echo "$i" # just to show that an * is enough (and accepts spaces).
done
And to process each file listed (which include directories) you should check that the filename is a file (not a directory) and also check if the directory doesn't exist before creating it.
for i in *
do
if [ -f "$i" ]; then
dir=${i%"${i#?}"}
if [ ! -d "$dir" ]; then
mkdir -m777 -p "$dir"
fi
mv "$i" "$dir"
if [ "$?" -ne 0 ]; then
echo "An error occurred moving file "$i" to dir "$dir""
fi
fi
done
edited 3 hours ago
answered 3 hours ago
IsaacIsaac
12.4k11955
12.4k11955
add a comment |
add a comment |
With GNU Parallel it looks like this:
parallel 'mkdir -p {=s/(.).*/$1/=}; mv {} {=s/(.).*/$1/=}' ::: *
(Edit: Just noted you are asking for files - not dirs. / is removed).
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
add a comment |
With GNU Parallel it looks like this:
parallel 'mkdir -p {=s/(.).*/$1/=}; mv {} {=s/(.).*/$1/=}' ::: *
(Edit: Just noted you are asking for files - not dirs. / is removed).
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
add a comment |
With GNU Parallel it looks like this:
parallel 'mkdir -p {=s/(.).*/$1/=}; mv {} {=s/(.).*/$1/=}' ::: *
(Edit: Just noted you are asking for files - not dirs. / is removed).
With GNU Parallel it looks like this:
parallel 'mkdir -p {=s/(.).*/$1/=}; mv {} {=s/(.).*/$1/=}' ::: *
(Edit: Just noted you are asking for files - not dirs. / is removed).
edited 3 hours ago
answered 4 hours ago
Ole TangeOle Tange
13.2k1658107
13.2k1658107
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
add a comment |
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
When I try this, I get the error: mv: cannot move '/' to a subdirectory of itself, '*/'
– Logan
3 hours ago
add a comment |
Logan is a new contributor. Be nice, and check out our Code of Conduct.
Logan is a new contributor. Be nice, and check out our Code of Conduct.
Logan is a new contributor. Be nice, and check out our Code of Conduct.
Logan is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f517665%2floop-over-files-with-spaces-in-their-names-create-new-folder-from-first-charact%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
for i in *; do ...
- see Bash Pitfall #1– steeldriver
4 hours ago
Welcome to Unix & Linux! It is generally a really bad idea to parse the output of
ls
. You should probably look into either usingfind
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here.– DopeGhoti
4 hours ago
Also, this will fail if you already (for example) have a file called
t
.– DopeGhoti
4 hours ago