Create folders from space-delimited filenames and copy files into them
I'm currently trying to organize several thousand files which are named according to what's in them, and the various "tags," if you will, are separated by spaces. So, for example:
foo_bar bar_foo.txt
I'm relatively new to Linux/Unix, and was wondering if there was a way to iterate through every file, create folders based on the tags, and copy the files to those folders?
So we'd end up with:
./foo_bar bar_foo.txt
./foo_bar/foo_bar bar_foo.txt
./bar_foo/foo_bar bar_foo.txt
So far, I've been manually doing everything like this:
mkdir foo_bar
cp *foo_bar* foo_bar/
mkdir bar_foo
cp *bar_foo* bar_foo/
...
Obviously this is pretty time-inefficient, so I'm just looking for a way to automatically do it.
Edit: Some more examples:
Input:
./a b c d.txt
./b a d.txt
./c d e.txt
./d a.txt
Output:
All original files still in parent directory, plus:
./a/a b c d.txt
./a/b a d.txt
./a/d a.txt
./b/a b c d.txt
./b/b a d.txt
./c/a b c d.txt
./c/c d e.txt
./d/a b c d.txt
./d/b a d.txt
./d/c d e.txt
./d/d a.txt
./e/c d e.txt
bash filenames file-copy
add a comment |
I'm currently trying to organize several thousand files which are named according to what's in them, and the various "tags," if you will, are separated by spaces. So, for example:
foo_bar bar_foo.txt
I'm relatively new to Linux/Unix, and was wondering if there was a way to iterate through every file, create folders based on the tags, and copy the files to those folders?
So we'd end up with:
./foo_bar bar_foo.txt
./foo_bar/foo_bar bar_foo.txt
./bar_foo/foo_bar bar_foo.txt
So far, I've been manually doing everything like this:
mkdir foo_bar
cp *foo_bar* foo_bar/
mkdir bar_foo
cp *bar_foo* bar_foo/
...
Obviously this is pretty time-inefficient, so I'm just looking for a way to automatically do it.
Edit: Some more examples:
Input:
./a b c d.txt
./b a d.txt
./c d e.txt
./d a.txt
Output:
All original files still in parent directory, plus:
./a/a b c d.txt
./a/b a d.txt
./a/d a.txt
./b/a b c d.txt
./b/b a d.txt
./c/a b c d.txt
./c/c d e.txt
./d/a b c d.txt
./d/b a d.txt
./d/c d e.txt
./d/d a.txt
./e/c d e.txt
bash filenames file-copy
add a comment |
I'm currently trying to organize several thousand files which are named according to what's in them, and the various "tags," if you will, are separated by spaces. So, for example:
foo_bar bar_foo.txt
I'm relatively new to Linux/Unix, and was wondering if there was a way to iterate through every file, create folders based on the tags, and copy the files to those folders?
So we'd end up with:
./foo_bar bar_foo.txt
./foo_bar/foo_bar bar_foo.txt
./bar_foo/foo_bar bar_foo.txt
So far, I've been manually doing everything like this:
mkdir foo_bar
cp *foo_bar* foo_bar/
mkdir bar_foo
cp *bar_foo* bar_foo/
...
Obviously this is pretty time-inefficient, so I'm just looking for a way to automatically do it.
Edit: Some more examples:
Input:
./a b c d.txt
./b a d.txt
./c d e.txt
./d a.txt
Output:
All original files still in parent directory, plus:
./a/a b c d.txt
./a/b a d.txt
./a/d a.txt
./b/a b c d.txt
./b/b a d.txt
./c/a b c d.txt
./c/c d e.txt
./d/a b c d.txt
./d/b a d.txt
./d/c d e.txt
./d/d a.txt
./e/c d e.txt
bash filenames file-copy
I'm currently trying to organize several thousand files which are named according to what's in them, and the various "tags," if you will, are separated by spaces. So, for example:
foo_bar bar_foo.txt
I'm relatively new to Linux/Unix, and was wondering if there was a way to iterate through every file, create folders based on the tags, and copy the files to those folders?
So we'd end up with:
./foo_bar bar_foo.txt
./foo_bar/foo_bar bar_foo.txt
./bar_foo/foo_bar bar_foo.txt
So far, I've been manually doing everything like this:
mkdir foo_bar
cp *foo_bar* foo_bar/
mkdir bar_foo
cp *bar_foo* bar_foo/
...
Obviously this is pretty time-inefficient, so I'm just looking for a way to automatically do it.
Edit: Some more examples:
Input:
./a b c d.txt
./b a d.txt
./c d e.txt
./d a.txt
Output:
All original files still in parent directory, plus:
./a/a b c d.txt
./a/b a d.txt
./a/d a.txt
./b/a b c d.txt
./b/b a d.txt
./c/a b c d.txt
./c/c d e.txt
./d/a b c d.txt
./d/b a d.txt
./d/c d e.txt
./d/d a.txt
./e/c d e.txt
bash filenames file-copy
bash filenames file-copy
edited 1 hour ago
Rui F Ribeiro
39.6k1479132
39.6k1479132
asked Dec 9 '16 at 9:34
SwammySwammy
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
try this.. remove the echo.. if you are happy with the commands...
ls * | awk '{print $1}' | sort -u | while read a; do echo mkdir -p $a; echo mv ${a}*txt ${a}; done
modified answer
$ ls
a b c d.txt b a d.txt c d e.txt d a.txt
$ ls * | sed "s/.txt//;s/ /n/g" | sort -u | while read file; do echo mkdir -p $file; echo mv *${file}*.txt ${file}; done
mkdir -p a
mv a b c d.txt b a d.txt d a.txt a
mkdir -p b
mv a b c d.txt b a d.txt b
mkdir -p c
mv a b c d.txt c d e.txt c
mkdir -p d
mv a b c d.txt b a d.txt c d e.txt d a.txt d
mkdir -p e
mv c d e.txt e
Note: as you want the file be present in your original directory.. use cp instead of mv
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
|
show 1 more 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
});
}
});
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%2f329149%2fcreate-folders-from-space-delimited-filenames-and-copy-files-into-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
try this.. remove the echo.. if you are happy with the commands...
ls * | awk '{print $1}' | sort -u | while read a; do echo mkdir -p $a; echo mv ${a}*txt ${a}; done
modified answer
$ ls
a b c d.txt b a d.txt c d e.txt d a.txt
$ ls * | sed "s/.txt//;s/ /n/g" | sort -u | while read file; do echo mkdir -p $file; echo mv *${file}*.txt ${file}; done
mkdir -p a
mv a b c d.txt b a d.txt d a.txt a
mkdir -p b
mv a b c d.txt b a d.txt b
mkdir -p c
mv a b c d.txt c d e.txt c
mkdir -p d
mv a b c d.txt b a d.txt c d e.txt d a.txt d
mkdir -p e
mv c d e.txt e
Note: as you want the file be present in your original directory.. use cp instead of mv
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
|
show 1 more comment
try this.. remove the echo.. if you are happy with the commands...
ls * | awk '{print $1}' | sort -u | while read a; do echo mkdir -p $a; echo mv ${a}*txt ${a}; done
modified answer
$ ls
a b c d.txt b a d.txt c d e.txt d a.txt
$ ls * | sed "s/.txt//;s/ /n/g" | sort -u | while read file; do echo mkdir -p $file; echo mv *${file}*.txt ${file}; done
mkdir -p a
mv a b c d.txt b a d.txt d a.txt a
mkdir -p b
mv a b c d.txt b a d.txt b
mkdir -p c
mv a b c d.txt c d e.txt c
mkdir -p d
mv a b c d.txt b a d.txt c d e.txt d a.txt d
mkdir -p e
mv c d e.txt e
Note: as you want the file be present in your original directory.. use cp instead of mv
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
|
show 1 more comment
try this.. remove the echo.. if you are happy with the commands...
ls * | awk '{print $1}' | sort -u | while read a; do echo mkdir -p $a; echo mv ${a}*txt ${a}; done
modified answer
$ ls
a b c d.txt b a d.txt c d e.txt d a.txt
$ ls * | sed "s/.txt//;s/ /n/g" | sort -u | while read file; do echo mkdir -p $file; echo mv *${file}*.txt ${file}; done
mkdir -p a
mv a b c d.txt b a d.txt d a.txt a
mkdir -p b
mv a b c d.txt b a d.txt b
mkdir -p c
mv a b c d.txt c d e.txt c
mkdir -p d
mv a b c d.txt b a d.txt c d e.txt d a.txt d
mkdir -p e
mv c d e.txt e
Note: as you want the file be present in your original directory.. use cp instead of mv
try this.. remove the echo.. if you are happy with the commands...
ls * | awk '{print $1}' | sort -u | while read a; do echo mkdir -p $a; echo mv ${a}*txt ${a}; done
modified answer
$ ls
a b c d.txt b a d.txt c d e.txt d a.txt
$ ls * | sed "s/.txt//;s/ /n/g" | sort -u | while read file; do echo mkdir -p $file; echo mv *${file}*.txt ${file}; done
mkdir -p a
mv a b c d.txt b a d.txt d a.txt a
mkdir -p b
mv a b c d.txt b a d.txt b
mkdir -p c
mv a b c d.txt c d e.txt c
mkdir -p d
mv a b c d.txt b a d.txt c d e.txt d a.txt d
mkdir -p e
mv c d e.txt e
Note: as you want the file be present in your original directory.. use cp instead of mv
edited Dec 9 '16 at 10:11
answered Dec 9 '16 at 9:52
KamarajKamaraj
2,9441513
2,9441513
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
|
show 1 more comment
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
Well, that almost works, but it only sorts by the first tag, so if we had multiple files with the foo_bar tag, but it wasn't the first tag, they wouldn't end up in the same folder.
– Swammy
Dec 9 '16 at 9:55
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
can you add more input and expected output... more files and which folder it has to go...etc...
– Kamaraj
Dec 9 '16 at 9:57
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
Alright, I added a few more.
– Swammy
Dec 9 '16 at 10:03
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
check the modified answer
– Kamaraj
Dec 9 '16 at 10:11
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
If I just remove the various ".txt"s from your answer, will that make it work with every file type, or would it require further modification? I just used .txt in the examples because it's the most common of all the files we have, but there are some of other types.
– Swammy
Dec 9 '16 at 10:16
|
show 1 more comment
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%2f329149%2fcreate-folders-from-space-delimited-filenames-and-copy-files-into-them%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