Search folder, find and copy files to new folder corresponding file ending
up vote
5
down vote
favorite
I have manage to grab some data of a couple of hdd with photorec, but I can't figure out how the files are saved, and it not very convenient searching in those recup_dir.*
. So my thought is to categorize by file endings. So all *.gif end up in /home/mike/photorec/12gb/sorted/gif
or something like that. But I don't know how to search the root catalog, grab the file extension and move/cp to that folder (and if not present, create the folder).
This way I can just delete unnecessary folders/files like dll
.
Lets say I have three folders:
~/photorec/80gb
~/photorec/120gb
~/photorec/100gb
Photorec creates a large amount of folder naming recup_dir.1
, recup_dir.2
etc. like:
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f21750248.jpg
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── f21750296.jpg
I want to cd 80gb
, run a command or execute a bash script inside ~/photorec/80gb/
so that a new folder is created, sorted
, and inside sorted I get all files from recup_dirs
, sorted by the found files extensions.
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
├── sorted
├── gif
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── jpg
├── f21750248.jpg
├── f21750296.jpg
How can I achieve this?
Edit: This is not just photorec. It could be any folder ofc.
command-line bash
add a comment |
up vote
5
down vote
favorite
I have manage to grab some data of a couple of hdd with photorec, but I can't figure out how the files are saved, and it not very convenient searching in those recup_dir.*
. So my thought is to categorize by file endings. So all *.gif end up in /home/mike/photorec/12gb/sorted/gif
or something like that. But I don't know how to search the root catalog, grab the file extension and move/cp to that folder (and if not present, create the folder).
This way I can just delete unnecessary folders/files like dll
.
Lets say I have three folders:
~/photorec/80gb
~/photorec/120gb
~/photorec/100gb
Photorec creates a large amount of folder naming recup_dir.1
, recup_dir.2
etc. like:
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f21750248.jpg
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── f21750296.jpg
I want to cd 80gb
, run a command or execute a bash script inside ~/photorec/80gb/
so that a new folder is created, sorted
, and inside sorted I get all files from recup_dirs
, sorted by the found files extensions.
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
├── sorted
├── gif
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── jpg
├── f21750248.jpg
├── f21750296.jpg
How can I achieve this?
Edit: This is not just photorec. It could be any folder ofc.
command-line bash
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have manage to grab some data of a couple of hdd with photorec, but I can't figure out how the files are saved, and it not very convenient searching in those recup_dir.*
. So my thought is to categorize by file endings. So all *.gif end up in /home/mike/photorec/12gb/sorted/gif
or something like that. But I don't know how to search the root catalog, grab the file extension and move/cp to that folder (and if not present, create the folder).
This way I can just delete unnecessary folders/files like dll
.
Lets say I have three folders:
~/photorec/80gb
~/photorec/120gb
~/photorec/100gb
Photorec creates a large amount of folder naming recup_dir.1
, recup_dir.2
etc. like:
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f21750248.jpg
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── f21750296.jpg
I want to cd 80gb
, run a command or execute a bash script inside ~/photorec/80gb/
so that a new folder is created, sorted
, and inside sorted I get all files from recup_dirs
, sorted by the found files extensions.
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
├── sorted
├── gif
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── jpg
├── f21750248.jpg
├── f21750296.jpg
How can I achieve this?
Edit: This is not just photorec. It could be any folder ofc.
command-line bash
I have manage to grab some data of a couple of hdd with photorec, but I can't figure out how the files are saved, and it not very convenient searching in those recup_dir.*
. So my thought is to categorize by file endings. So all *.gif end up in /home/mike/photorec/12gb/sorted/gif
or something like that. But I don't know how to search the root catalog, grab the file extension and move/cp to that folder (and if not present, create the folder).
This way I can just delete unnecessary folders/files like dll
.
Lets say I have three folders:
~/photorec/80gb
~/photorec/120gb
~/photorec/100gb
Photorec creates a large amount of folder naming recup_dir.1
, recup_dir.2
etc. like:
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f21750248.jpg
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── f21750296.jpg
I want to cd 80gb
, run a command or execute a bash script inside ~/photorec/80gb/
so that a new folder is created, sorted
, and inside sorted I get all files from recup_dirs
, sorted by the found files extensions.
.
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
├── sorted
├── gif
├── f21750275.gif
├── f21750277.gif
├── f21750281.gif
├── jpg
├── f21750248.jpg
├── f21750296.jpg
How can I achieve this?
Edit: This is not just photorec. It could be any folder ofc.
command-line bash
command-line bash
edited 4 hours ago
asked 5 hours ago
Adam
221110
221110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
First, create some test files:
mkdir -p 80gb/recup_dir.{1,10,11,12,9}
touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
This gives:
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
Now:
find 80gb -type f -exec bash -c 'mkdir -p sorted/"${0##*.}"; mv "$0" sorted/"${0##*.}"' {} ;
$0
holds the current filename (the {}
parameter to the scriptlet) and
"${0##*.}"
is the file's extension.
Result:
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
If you want the sorted
directory below 80gb
it might
be easiest to do a cd 80gb
first and then do find . …
instead of find 80gb …
.
Instead of mv
you may first want to do cp
instead
in case anything goes wrong.
Caveat
The expression "${0##*.}"
only works for files with an extension.
For files without an extension it returns the complete filename (including the path)
and the command will fail. If you expect files without extensions modify the command
to:
find 80gb -type f -name "*.*" -exec … ;
so it catches only files with a dot in them.
2
Another way to test is to prependecho
to themv
command, so that you see what command would be executed
– Sergiy Kolodyazhnyy
3 hours ago
1
How about testing whether the dir exists and only call the external (!)mkdir
if not?[ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
2
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
add a comment |
up vote
3
down vote
Here’s a way which moves all files of one extension at once by using bash
’s globstar
option and looping over the different extensions:
shopt -s globstar
for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do
mkdir -p sorted/"$i";
mv **/*."$i" sorted/"$i";
done
The find | sed | sort
command list creates a list of existing file extensions over which the for
loop loops. For every extension, mkdir
creates a directory under sorted/
and m
ov
es the matching files to it. **/
is the globstar pattern and matches (theoretically) infinite directories and subdirectories, see man bash
/SHELL BUILTIN COMMANDS under shopt/globstar.
Example run
$ mkdir -p 80gb/recup_dir.{1,10,11,12,9}
$ touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
$ tree
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
$ shopt -s globstar; for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do mkdir -p sorted/"$i"; mv **/*."$i" sorted/"$i"; done
$ tree
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
Nice. Especially that you search*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the${0##*.}
is wrong then).
– PerlDuck
2 hours ago
1
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1102279%2fsearch-folder-find-and-copy-files-to-new-folder-corresponding-file-ending%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
up vote
4
down vote
First, create some test files:
mkdir -p 80gb/recup_dir.{1,10,11,12,9}
touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
This gives:
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
Now:
find 80gb -type f -exec bash -c 'mkdir -p sorted/"${0##*.}"; mv "$0" sorted/"${0##*.}"' {} ;
$0
holds the current filename (the {}
parameter to the scriptlet) and
"${0##*.}"
is the file's extension.
Result:
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
If you want the sorted
directory below 80gb
it might
be easiest to do a cd 80gb
first and then do find . …
instead of find 80gb …
.
Instead of mv
you may first want to do cp
instead
in case anything goes wrong.
Caveat
The expression "${0##*.}"
only works for files with an extension.
For files without an extension it returns the complete filename (including the path)
and the command will fail. If you expect files without extensions modify the command
to:
find 80gb -type f -name "*.*" -exec … ;
so it catches only files with a dot in them.
2
Another way to test is to prependecho
to themv
command, so that you see what command would be executed
– Sergiy Kolodyazhnyy
3 hours ago
1
How about testing whether the dir exists and only call the external (!)mkdir
if not?[ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
2
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
add a comment |
up vote
4
down vote
First, create some test files:
mkdir -p 80gb/recup_dir.{1,10,11,12,9}
touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
This gives:
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
Now:
find 80gb -type f -exec bash -c 'mkdir -p sorted/"${0##*.}"; mv "$0" sorted/"${0##*.}"' {} ;
$0
holds the current filename (the {}
parameter to the scriptlet) and
"${0##*.}"
is the file's extension.
Result:
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
If you want the sorted
directory below 80gb
it might
be easiest to do a cd 80gb
first and then do find . …
instead of find 80gb …
.
Instead of mv
you may first want to do cp
instead
in case anything goes wrong.
Caveat
The expression "${0##*.}"
only works for files with an extension.
For files without an extension it returns the complete filename (including the path)
and the command will fail. If you expect files without extensions modify the command
to:
find 80gb -type f -name "*.*" -exec … ;
so it catches only files with a dot in them.
2
Another way to test is to prependecho
to themv
command, so that you see what command would be executed
– Sergiy Kolodyazhnyy
3 hours ago
1
How about testing whether the dir exists and only call the external (!)mkdir
if not?[ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
2
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
add a comment |
up vote
4
down vote
up vote
4
down vote
First, create some test files:
mkdir -p 80gb/recup_dir.{1,10,11,12,9}
touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
This gives:
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
Now:
find 80gb -type f -exec bash -c 'mkdir -p sorted/"${0##*.}"; mv "$0" sorted/"${0##*.}"' {} ;
$0
holds the current filename (the {}
parameter to the scriptlet) and
"${0##*.}"
is the file's extension.
Result:
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
If you want the sorted
directory below 80gb
it might
be easiest to do a cd 80gb
first and then do find . …
instead of find 80gb …
.
Instead of mv
you may first want to do cp
instead
in case anything goes wrong.
Caveat
The expression "${0##*.}"
only works for files with an extension.
For files without an extension it returns the complete filename (including the path)
and the command will fail. If you expect files without extensions modify the command
to:
find 80gb -type f -name "*.*" -exec … ;
so it catches only files with a dot in them.
First, create some test files:
mkdir -p 80gb/recup_dir.{1,10,11,12,9}
touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
This gives:
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
Now:
find 80gb -type f -exec bash -c 'mkdir -p sorted/"${0##*.}"; mv "$0" sorted/"${0##*.}"' {} ;
$0
holds the current filename (the {}
parameter to the scriptlet) and
"${0##*.}"
is the file's extension.
Result:
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
If you want the sorted
directory below 80gb
it might
be easiest to do a cd 80gb
first and then do find . …
instead of find 80gb …
.
Instead of mv
you may first want to do cp
instead
in case anything goes wrong.
Caveat
The expression "${0##*.}"
only works for files with an extension.
For files without an extension it returns the complete filename (including the path)
and the command will fail. If you expect files without extensions modify the command
to:
find 80gb -type f -name "*.*" -exec … ;
so it catches only files with a dot in them.
edited 2 hours ago
answered 3 hours ago
PerlDuck
5,13911230
5,13911230
2
Another way to test is to prependecho
to themv
command, so that you see what command would be executed
– Sergiy Kolodyazhnyy
3 hours ago
1
How about testing whether the dir exists and only call the external (!)mkdir
if not?[ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
2
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
add a comment |
2
Another way to test is to prependecho
to themv
command, so that you see what command would be executed
– Sergiy Kolodyazhnyy
3 hours ago
1
How about testing whether the dir exists and only call the external (!)mkdir
if not?[ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
2
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
2
2
Another way to test is to prepend
echo
to the mv
command, so that you see what command would be executed– Sergiy Kolodyazhnyy
3 hours ago
Another way to test is to prepend
echo
to the mv
command, so that you see what command would be executed– Sergiy Kolodyazhnyy
3 hours ago
1
1
How about testing whether the dir exists and only call the external (!)
mkdir
if not? [ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
How about testing whether the dir exists and only call the external (!)
mkdir
if not? [ -d sorted/"${0##*.}" ] || mkdir …
– dessert
3 hours ago
2
2
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
Yes, @dessert, that's better if speed matters – which I doubt in this particular case. I was lazy and wanted to avoid an overly long command with multiple ugly expressions in it ;-)
– PerlDuck
3 hours ago
add a comment |
up vote
3
down vote
Here’s a way which moves all files of one extension at once by using bash
’s globstar
option and looping over the different extensions:
shopt -s globstar
for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do
mkdir -p sorted/"$i";
mv **/*."$i" sorted/"$i";
done
The find | sed | sort
command list creates a list of existing file extensions over which the for
loop loops. For every extension, mkdir
creates a directory under sorted/
and m
ov
es the matching files to it. **/
is the globstar pattern and matches (theoretically) infinite directories and subdirectories, see man bash
/SHELL BUILTIN COMMANDS under shopt/globstar.
Example run
$ mkdir -p 80gb/recup_dir.{1,10,11,12,9}
$ touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
$ tree
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
$ shopt -s globstar; for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do mkdir -p sorted/"$i"; mv **/*."$i" sorted/"$i"; done
$ tree
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
Nice. Especially that you search*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the${0##*.}
is wrong then).
– PerlDuck
2 hours ago
1
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
add a comment |
up vote
3
down vote
Here’s a way which moves all files of one extension at once by using bash
’s globstar
option and looping over the different extensions:
shopt -s globstar
for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do
mkdir -p sorted/"$i";
mv **/*."$i" sorted/"$i";
done
The find | sed | sort
command list creates a list of existing file extensions over which the for
loop loops. For every extension, mkdir
creates a directory under sorted/
and m
ov
es the matching files to it. **/
is the globstar pattern and matches (theoretically) infinite directories and subdirectories, see man bash
/SHELL BUILTIN COMMANDS under shopt/globstar.
Example run
$ mkdir -p 80gb/recup_dir.{1,10,11,12,9}
$ touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
$ tree
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
$ shopt -s globstar; for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do mkdir -p sorted/"$i"; mv **/*."$i" sorted/"$i"; done
$ tree
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
Nice. Especially that you search*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the${0##*.}
is wrong then).
– PerlDuck
2 hours ago
1
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
add a comment |
up vote
3
down vote
up vote
3
down vote
Here’s a way which moves all files of one extension at once by using bash
’s globstar
option and looping over the different extensions:
shopt -s globstar
for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do
mkdir -p sorted/"$i";
mv **/*."$i" sorted/"$i";
done
The find | sed | sort
command list creates a list of existing file extensions over which the for
loop loops. For every extension, mkdir
creates a directory under sorted/
and m
ov
es the matching files to it. **/
is the globstar pattern and matches (theoretically) infinite directories and subdirectories, see man bash
/SHELL BUILTIN COMMANDS under shopt/globstar.
Example run
$ mkdir -p 80gb/recup_dir.{1,10,11,12,9}
$ touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
$ tree
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
$ shopt -s globstar; for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do mkdir -p sorted/"$i"; mv **/*."$i" sorted/"$i"; done
$ tree
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
Here’s a way which moves all files of one extension at once by using bash
’s globstar
option and looping over the different extensions:
shopt -s globstar
for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do
mkdir -p sorted/"$i";
mv **/*."$i" sorted/"$i";
done
The find | sed | sort
command list creates a list of existing file extensions over which the for
loop loops. For every extension, mkdir
creates a directory under sorted/
and m
ov
es the matching files to it. **/
is the globstar pattern and matches (theoretically) infinite directories and subdirectories, see man bash
/SHELL BUILTIN COMMANDS under shopt/globstar.
Example run
$ mkdir -p 80gb/recup_dir.{1,10,11,12,9}
$ touch 80gb/recup_dir.9/f00{1..3}.{jpg,png,gif}
$ tree
.
└── 80gb
├── recup_dir.1
├── recup_dir.10
├── recup_dir.11
├── recup_dir.12
└── recup_dir.9
├── f001.gif
├── f001.jpg
├── f001.png
├── f002.gif
├── f002.jpg
├── f002.png
├── f003.gif
├── f003.jpg
└── f003.png
$ shopt -s globstar; for i in $(find -type f -name "*.*" -printf '%pn' | sed 's/.*.//' | sort -u); do mkdir -p sorted/"$i"; mv **/*."$i" sorted/"$i"; done
$ tree
.
├── 80gb
│ ├── recup_dir.1
│ ├── recup_dir.10
│ ├── recup_dir.11
│ ├── recup_dir.12
│ └── recup_dir.9
└── sorted
├── gif
│ ├── f001.gif
│ ├── f002.gif
│ └── f003.gif
├── jpg
│ ├── f001.jpg
│ ├── f002.jpg
│ └── f003.jpg
└── png
├── f001.png
├── f002.png
└── f003.png
answered 3 hours ago
dessert
21.7k55896
21.7k55896
Nice. Especially that you search*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the${0##*.}
is wrong then).
– PerlDuck
2 hours ago
1
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
add a comment |
Nice. Especially that you search*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the${0##*.}
is wrong then).
– PerlDuck
2 hours ago
1
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
Nice. Especially that you search
*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the ${0##*.}
is wrong then).– PerlDuck
2 hours ago
Nice. Especially that you search
*.*
, i.e. just files with an extension. My solution fails for files which don't have one (the ${0##*.}
is wrong then).– PerlDuck
2 hours ago
1
1
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
Done. I didn't add a "real" workaround, just a way to omit those extensionless files.
– PerlDuck
2 hours ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
@PerlDuck Nicely done! :)
– dessert
1 hour ago
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2faskubuntu.com%2fquestions%2f1102279%2fsearch-folder-find-and-copy-files-to-new-folder-corresponding-file-ending%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