Move all files NOT ending with .txt
In the directory /home/username/data
I have both files and directories. Some of these filenames end in .txt
(to which I'll refer as text files), others don't. The same happens in the subdirectories.
One of the subdirectories is called other_files
(its full path is /home/username/data/other_files/
).
I'd like to move all the files not ending with .txt
in the root of /home/username/data
to other_files
.
I could possibly do it with a loop, but that's not what I want. I want to use commands and piping. I believe this is easy, I'm just not seeing it. A combination of mv
, find
, grep
and xargs
should do it, I'm just not sure how.
So I'm stuck in trying to match the text files (to then think of way to match everything except them). In the following, assume my current directory is /home/username/data
.
First I went for find . | grep -E "*.txt"
, but this matches all text files, including the ones in the subdirectories.
So I tried find . | grep -E "./*.txt"
just to see if I would get the same matches to then work my way towards my goal, but this doesn't match anything and this is where I'm stuck.
How do I go about doing what I described at the beginning of the question?
bash grep find filenames xargs
New contributor
add a comment |
In the directory /home/username/data
I have both files and directories. Some of these filenames end in .txt
(to which I'll refer as text files), others don't. The same happens in the subdirectories.
One of the subdirectories is called other_files
(its full path is /home/username/data/other_files/
).
I'd like to move all the files not ending with .txt
in the root of /home/username/data
to other_files
.
I could possibly do it with a loop, but that's not what I want. I want to use commands and piping. I believe this is easy, I'm just not seeing it. A combination of mv
, find
, grep
and xargs
should do it, I'm just not sure how.
So I'm stuck in trying to match the text files (to then think of way to match everything except them). In the following, assume my current directory is /home/username/data
.
First I went for find . | grep -E "*.txt"
, but this matches all text files, including the ones in the subdirectories.
So I tried find . | grep -E "./*.txt"
just to see if I would get the same matches to then work my way towards my goal, but this doesn't match anything and this is where I'm stuck.
How do I go about doing what I described at the beginning of the question?
bash grep find filenames xargs
New contributor
so you would like to move all files which are not ending with.txt
from currunt directory/home/username/data
to its sub-directory/home/username/data/other_files
'.... am i right?
– msp9011
1 hour ago
find DIR ! -name '*.txt'
might help. Also can you add an example of source and target structure? Right now it's not clear whether the other directories beneath/home/username/data
need to be recreated beneath/home/username/data/other_files/
.
– nohillside
1 hour ago
Related: unix.stackexchange.com/q/154818/315749 (You would just need to adapt it by negating the-name
test with a!
and maybe adding a-type f
test to match only regular files).
– fra-san
1 hour ago
@msp9011 You're correct.
– Moving Man
1 hour ago
@nohillside They don't need to be recreated withinother_files
because I only want to move files that are directly on the root ofhome/username/data
.
– Moving Man
1 hour ago
add a comment |
In the directory /home/username/data
I have both files and directories. Some of these filenames end in .txt
(to which I'll refer as text files), others don't. The same happens in the subdirectories.
One of the subdirectories is called other_files
(its full path is /home/username/data/other_files/
).
I'd like to move all the files not ending with .txt
in the root of /home/username/data
to other_files
.
I could possibly do it with a loop, but that's not what I want. I want to use commands and piping. I believe this is easy, I'm just not seeing it. A combination of mv
, find
, grep
and xargs
should do it, I'm just not sure how.
So I'm stuck in trying to match the text files (to then think of way to match everything except them). In the following, assume my current directory is /home/username/data
.
First I went for find . | grep -E "*.txt"
, but this matches all text files, including the ones in the subdirectories.
So I tried find . | grep -E "./*.txt"
just to see if I would get the same matches to then work my way towards my goal, but this doesn't match anything and this is where I'm stuck.
How do I go about doing what I described at the beginning of the question?
bash grep find filenames xargs
New contributor
In the directory /home/username/data
I have both files and directories. Some of these filenames end in .txt
(to which I'll refer as text files), others don't. The same happens in the subdirectories.
One of the subdirectories is called other_files
(its full path is /home/username/data/other_files/
).
I'd like to move all the files not ending with .txt
in the root of /home/username/data
to other_files
.
I could possibly do it with a loop, but that's not what I want. I want to use commands and piping. I believe this is easy, I'm just not seeing it. A combination of mv
, find
, grep
and xargs
should do it, I'm just not sure how.
So I'm stuck in trying to match the text files (to then think of way to match everything except them). In the following, assume my current directory is /home/username/data
.
First I went for find . | grep -E "*.txt"
, but this matches all text files, including the ones in the subdirectories.
So I tried find . | grep -E "./*.txt"
just to see if I would get the same matches to then work my way towards my goal, but this doesn't match anything and this is where I'm stuck.
How do I go about doing what I described at the beginning of the question?
bash grep find filenames xargs
bash grep find filenames xargs
New contributor
New contributor
edited 42 mins ago
Jeff Schaller
38.8k1053125
38.8k1053125
New contributor
asked 2 hours ago
Moving Man
163
163
New contributor
New contributor
so you would like to move all files which are not ending with.txt
from currunt directory/home/username/data
to its sub-directory/home/username/data/other_files
'.... am i right?
– msp9011
1 hour ago
find DIR ! -name '*.txt'
might help. Also can you add an example of source and target structure? Right now it's not clear whether the other directories beneath/home/username/data
need to be recreated beneath/home/username/data/other_files/
.
– nohillside
1 hour ago
Related: unix.stackexchange.com/q/154818/315749 (You would just need to adapt it by negating the-name
test with a!
and maybe adding a-type f
test to match only regular files).
– fra-san
1 hour ago
@msp9011 You're correct.
– Moving Man
1 hour ago
@nohillside They don't need to be recreated withinother_files
because I only want to move files that are directly on the root ofhome/username/data
.
– Moving Man
1 hour ago
add a comment |
so you would like to move all files which are not ending with.txt
from currunt directory/home/username/data
to its sub-directory/home/username/data/other_files
'.... am i right?
– msp9011
1 hour ago
find DIR ! -name '*.txt'
might help. Also can you add an example of source and target structure? Right now it's not clear whether the other directories beneath/home/username/data
need to be recreated beneath/home/username/data/other_files/
.
– nohillside
1 hour ago
Related: unix.stackexchange.com/q/154818/315749 (You would just need to adapt it by negating the-name
test with a!
and maybe adding a-type f
test to match only regular files).
– fra-san
1 hour ago
@msp9011 You're correct.
– Moving Man
1 hour ago
@nohillside They don't need to be recreated withinother_files
because I only want to move files that are directly on the root ofhome/username/data
.
– Moving Man
1 hour ago
so you would like to move all files which are not ending with
.txt
from currunt directory /home/username/data
to its sub-directory /home/username/data/other_files
'.... am i right?– msp9011
1 hour ago
so you would like to move all files which are not ending with
.txt
from currunt directory /home/username/data
to its sub-directory /home/username/data/other_files
'.... am i right?– msp9011
1 hour ago
find DIR ! -name '*.txt'
might help. Also can you add an example of source and target structure? Right now it's not clear whether the other directories beneath /home/username/data
need to be recreated beneath /home/username/data/other_files/
.– nohillside
1 hour ago
find DIR ! -name '*.txt'
might help. Also can you add an example of source and target structure? Right now it's not clear whether the other directories beneath /home/username/data
need to be recreated beneath /home/username/data/other_files/
.– nohillside
1 hour ago
Related: unix.stackexchange.com/q/154818/315749 (You would just need to adapt it by negating the
-name
test with a !
and maybe adding a -type f
test to match only regular files).– fra-san
1 hour ago
Related: unix.stackexchange.com/q/154818/315749 (You would just need to adapt it by negating the
-name
test with a !
and maybe adding a -type f
test to match only regular files).– fra-san
1 hour ago
@msp9011 You're correct.
– Moving Man
1 hour ago
@msp9011 You're correct.
– Moving Man
1 hour ago
@nohillside They don't need to be recreated within
other_files
because I only want to move files that are directly on the root of home/username/data
.– Moving Man
1 hour ago
@nohillside They don't need to be recreated within
other_files
because I only want to move files that are directly on the root of home/username/data
.– Moving Man
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
The simple shell loop variant (in bash
):
shopt -s extglob dotglob nullglob
for pathname in ~username/data/!(*.txt); do
! test -d "$pathname" && mv "$pathname" ~username/data/other_files
done
The shell options set on the first line will make the bash
shell enable extended globbing patterns (!(*.txt)
to match all names not ending with .txt
), it enables glob patterns to match hidden names, and it makes the pattern expand to nothing at all if nothing matches.
The body of the loop will skip anything that is a directory (or symbolic link to a directory) and will move everything else to the given directory.
The equivalent thing with find
and GNU mv
(will copy symbolic links to directories if there are any, and will invoke mv
for as many files as possible at a time, but those are the only differences):
find ~username/data -maxdepth 1 ! -type d ! -name '*.txt'
-exec mv -t ~username/data/other_files {} +
Related:
- Understanding the -exec option of `find`
In your second solution you're not accounting for*.txt
, are you?
– Moving Man
57 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
Thank you very much.
– Moving Man
55 mins ago
Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt"
) failed?
– Moving Man
48 mins ago
@MovingMan It assumes that the pathnames look like./.txt
where the/
is allowed to be repeated, as in./////.txt
. Use regular expressions on text and use filename globbing patterns (and notgrep
) on filenames.
– Kusalananda
46 mins ago
|
show 1 more comment
This code should move all files not ending in ".txt" to your target folder, however if you happen to have files with the same name in different paths it will throw an error.
find /home/username/data ! -name "*.txt" -type f -exec mv {} /home/username/data/other_files/ ;
Doesn‘t this also try to move the files put intoother_files
again?
– nohillside
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is ifother_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with! -path
.
– Tamas H.
1 hour ago
2
-maxdepth 1
to stop it from walking down into subdirectories.
– Kusalananda
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
|
show 1 more comment
find /home/username/data -maxdepth 1 -type f ! -name '*.txt' -exec mv {} /home/username/data/other_files/ ;
- maxdepth limits to the top directors
- type ensures that only files are found, not directories
2
You'd better use-type f
or it will try to moveother_files
, and any other subdirectory ofdata
that we don't know about.
– Kusalananda
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
I'm trying to figure out the behavior of-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory toother_files
? The syntax for move, usually, ismv source directory
.
– Moving Man
1 hour ago
@MovingMan The{}
will replaced by the current pathname thatfind
is looking at.
– Kusalananda
1 hour ago
1
@MovingMan-exec
takes a utility and arguments. To know where that command line ends,find
looks for;
. The;
needs to be escaped to protect it from the shell.
– Kusalananda
59 mins ago
|
show 4 more comments
The following line finds all files and hidden files in the current directory that are not *.txt
and not a path and move them into newpath:
ls -1p | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
The following is the same but moves also hidden files:
ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
Both command lines don't scan directories recursively and don't move directories
1
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
This isn't an issue for find (use-exec ... {}
or-print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)
– nohillside
1 min 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
});
}
});
Moving Man 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%2f492006%2fmove-all-files-not-ending-with-txt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The simple shell loop variant (in bash
):
shopt -s extglob dotglob nullglob
for pathname in ~username/data/!(*.txt); do
! test -d "$pathname" && mv "$pathname" ~username/data/other_files
done
The shell options set on the first line will make the bash
shell enable extended globbing patterns (!(*.txt)
to match all names not ending with .txt
), it enables glob patterns to match hidden names, and it makes the pattern expand to nothing at all if nothing matches.
The body of the loop will skip anything that is a directory (or symbolic link to a directory) and will move everything else to the given directory.
The equivalent thing with find
and GNU mv
(will copy symbolic links to directories if there are any, and will invoke mv
for as many files as possible at a time, but those are the only differences):
find ~username/data -maxdepth 1 ! -type d ! -name '*.txt'
-exec mv -t ~username/data/other_files {} +
Related:
- Understanding the -exec option of `find`
In your second solution you're not accounting for*.txt
, are you?
– Moving Man
57 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
Thank you very much.
– Moving Man
55 mins ago
Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt"
) failed?
– Moving Man
48 mins ago
@MovingMan It assumes that the pathnames look like./.txt
where the/
is allowed to be repeated, as in./////.txt
. Use regular expressions on text and use filename globbing patterns (and notgrep
) on filenames.
– Kusalananda
46 mins ago
|
show 1 more comment
The simple shell loop variant (in bash
):
shopt -s extglob dotglob nullglob
for pathname in ~username/data/!(*.txt); do
! test -d "$pathname" && mv "$pathname" ~username/data/other_files
done
The shell options set on the first line will make the bash
shell enable extended globbing patterns (!(*.txt)
to match all names not ending with .txt
), it enables glob patterns to match hidden names, and it makes the pattern expand to nothing at all if nothing matches.
The body of the loop will skip anything that is a directory (or symbolic link to a directory) and will move everything else to the given directory.
The equivalent thing with find
and GNU mv
(will copy symbolic links to directories if there are any, and will invoke mv
for as many files as possible at a time, but those are the only differences):
find ~username/data -maxdepth 1 ! -type d ! -name '*.txt'
-exec mv -t ~username/data/other_files {} +
Related:
- Understanding the -exec option of `find`
In your second solution you're not accounting for*.txt
, are you?
– Moving Man
57 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
Thank you very much.
– Moving Man
55 mins ago
Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt"
) failed?
– Moving Man
48 mins ago
@MovingMan It assumes that the pathnames look like./.txt
where the/
is allowed to be repeated, as in./////.txt
. Use regular expressions on text and use filename globbing patterns (and notgrep
) on filenames.
– Kusalananda
46 mins ago
|
show 1 more comment
The simple shell loop variant (in bash
):
shopt -s extglob dotglob nullglob
for pathname in ~username/data/!(*.txt); do
! test -d "$pathname" && mv "$pathname" ~username/data/other_files
done
The shell options set on the first line will make the bash
shell enable extended globbing patterns (!(*.txt)
to match all names not ending with .txt
), it enables glob patterns to match hidden names, and it makes the pattern expand to nothing at all if nothing matches.
The body of the loop will skip anything that is a directory (or symbolic link to a directory) and will move everything else to the given directory.
The equivalent thing with find
and GNU mv
(will copy symbolic links to directories if there are any, and will invoke mv
for as many files as possible at a time, but those are the only differences):
find ~username/data -maxdepth 1 ! -type d ! -name '*.txt'
-exec mv -t ~username/data/other_files {} +
Related:
- Understanding the -exec option of `find`
The simple shell loop variant (in bash
):
shopt -s extglob dotglob nullglob
for pathname in ~username/data/!(*.txt); do
! test -d "$pathname" && mv "$pathname" ~username/data/other_files
done
The shell options set on the first line will make the bash
shell enable extended globbing patterns (!(*.txt)
to match all names not ending with .txt
), it enables glob patterns to match hidden names, and it makes the pattern expand to nothing at all if nothing matches.
The body of the loop will skip anything that is a directory (or symbolic link to a directory) and will move everything else to the given directory.
The equivalent thing with find
and GNU mv
(will copy symbolic links to directories if there are any, and will invoke mv
for as many files as possible at a time, but those are the only differences):
find ~username/data -maxdepth 1 ! -type d ! -name '*.txt'
-exec mv -t ~username/data/other_files {} +
Related:
- Understanding the -exec option of `find`
edited 54 mins ago
answered 1 hour ago
Kusalananda
122k16229372
122k16229372
In your second solution you're not accounting for*.txt
, are you?
– Moving Man
57 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
Thank you very much.
– Moving Man
55 mins ago
Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt"
) failed?
– Moving Man
48 mins ago
@MovingMan It assumes that the pathnames look like./.txt
where the/
is allowed to be repeated, as in./////.txt
. Use regular expressions on text and use filename globbing patterns (and notgrep
) on filenames.
– Kusalananda
46 mins ago
|
show 1 more comment
In your second solution you're not accounting for*.txt
, are you?
– Moving Man
57 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
Thank you very much.
– Moving Man
55 mins ago
Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt"
) failed?
– Moving Man
48 mins ago
@MovingMan It assumes that the pathnames look like./.txt
where the/
is allowed to be repeated, as in./////.txt
. Use regular expressions on text and use filename globbing patterns (and notgrep
) on filenames.
– Kusalananda
46 mins ago
In your second solution you're not accounting for
*.txt
, are you?– Moving Man
57 mins ago
In your second solution you're not accounting for
*.txt
, are you?– Moving Man
57 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
56 mins ago
Thank you very much.
– Moving Man
55 mins ago
Thank you very much.
– Moving Man
55 mins ago
Side question, do you know why the regex I mentioned (
find . | grep -E "./*.txt"
) failed?– Moving Man
48 mins ago
Side question, do you know why the regex I mentioned (
find . | grep -E "./*.txt"
) failed?– Moving Man
48 mins ago
@MovingMan It assumes that the pathnames look like
./.txt
where the /
is allowed to be repeated, as in ./////.txt
. Use regular expressions on text and use filename globbing patterns (and not grep
) on filenames.– Kusalananda
46 mins ago
@MovingMan It assumes that the pathnames look like
./.txt
where the /
is allowed to be repeated, as in ./////.txt
. Use regular expressions on text and use filename globbing patterns (and not grep
) on filenames.– Kusalananda
46 mins ago
|
show 1 more comment
This code should move all files not ending in ".txt" to your target folder, however if you happen to have files with the same name in different paths it will throw an error.
find /home/username/data ! -name "*.txt" -type f -exec mv {} /home/username/data/other_files/ ;
Doesn‘t this also try to move the files put intoother_files
again?
– nohillside
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is ifother_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with! -path
.
– Tamas H.
1 hour ago
2
-maxdepth 1
to stop it from walking down into subdirectories.
– Kusalananda
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
|
show 1 more comment
This code should move all files not ending in ".txt" to your target folder, however if you happen to have files with the same name in different paths it will throw an error.
find /home/username/data ! -name "*.txt" -type f -exec mv {} /home/username/data/other_files/ ;
Doesn‘t this also try to move the files put intoother_files
again?
– nohillside
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is ifother_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with! -path
.
– Tamas H.
1 hour ago
2
-maxdepth 1
to stop it from walking down into subdirectories.
– Kusalananda
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
|
show 1 more comment
This code should move all files not ending in ".txt" to your target folder, however if you happen to have files with the same name in different paths it will throw an error.
find /home/username/data ! -name "*.txt" -type f -exec mv {} /home/username/data/other_files/ ;
This code should move all files not ending in ".txt" to your target folder, however if you happen to have files with the same name in different paths it will throw an error.
find /home/username/data ! -name "*.txt" -type f -exec mv {} /home/username/data/other_files/ ;
answered 1 hour ago
Tamas H.
664
664
Doesn‘t this also try to move the files put intoother_files
again?
– nohillside
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is ifother_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with! -path
.
– Tamas H.
1 hour ago
2
-maxdepth 1
to stop it from walking down into subdirectories.
– Kusalananda
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
|
show 1 more comment
Doesn‘t this also try to move the files put intoother_files
again?
– nohillside
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is ifother_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with! -path
.
– Tamas H.
1 hour ago
2
-maxdepth 1
to stop it from walking down into subdirectories.
– Kusalananda
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
Doesn‘t this also try to move the files put into
other_files
again?– nohillside
1 hour ago
Doesn‘t this also try to move the files put into
other_files
again?– nohillside
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is if
other_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with ! -path
.– Tamas H.
1 hour ago
I think it will simply throw an error that the source and target is the same. Only case where this could be bad is if
other_files
has a directory structure that should not be touched. In this case this could be excluded from the find command with ! -path
.– Tamas H.
1 hour ago
2
2
-maxdepth 1
to stop it from walking down into subdirectories.– Kusalananda
1 hour ago
-maxdepth 1
to stop it from walking down into subdirectories.– Kusalananda
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Kusalananda is correct.
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
Can you please check my comment here?
– Moving Man
1 hour ago
|
show 1 more comment
find /home/username/data -maxdepth 1 -type f ! -name '*.txt' -exec mv {} /home/username/data/other_files/ ;
- maxdepth limits to the top directors
- type ensures that only files are found, not directories
2
You'd better use-type f
or it will try to moveother_files
, and any other subdirectory ofdata
that we don't know about.
– Kusalananda
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
I'm trying to figure out the behavior of-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory toother_files
? The syntax for move, usually, ismv source directory
.
– Moving Man
1 hour ago
@MovingMan The{}
will replaced by the current pathname thatfind
is looking at.
– Kusalananda
1 hour ago
1
@MovingMan-exec
takes a utility and arguments. To know where that command line ends,find
looks for;
. The;
needs to be escaped to protect it from the shell.
– Kusalananda
59 mins ago
|
show 4 more comments
find /home/username/data -maxdepth 1 -type f ! -name '*.txt' -exec mv {} /home/username/data/other_files/ ;
- maxdepth limits to the top directors
- type ensures that only files are found, not directories
2
You'd better use-type f
or it will try to moveother_files
, and any other subdirectory ofdata
that we don't know about.
– Kusalananda
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
I'm trying to figure out the behavior of-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory toother_files
? The syntax for move, usually, ismv source directory
.
– Moving Man
1 hour ago
@MovingMan The{}
will replaced by the current pathname thatfind
is looking at.
– Kusalananda
1 hour ago
1
@MovingMan-exec
takes a utility and arguments. To know where that command line ends,find
looks for;
. The;
needs to be escaped to protect it from the shell.
– Kusalananda
59 mins ago
|
show 4 more comments
find /home/username/data -maxdepth 1 -type f ! -name '*.txt' -exec mv {} /home/username/data/other_files/ ;
- maxdepth limits to the top directors
- type ensures that only files are found, not directories
find /home/username/data -maxdepth 1 -type f ! -name '*.txt' -exec mv {} /home/username/data/other_files/ ;
- maxdepth limits to the top directors
- type ensures that only files are found, not directories
edited 59 mins ago
answered 1 hour ago
nohillside
2,312819
2,312819
2
You'd better use-type f
or it will try to moveother_files
, and any other subdirectory ofdata
that we don't know about.
– Kusalananda
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
I'm trying to figure out the behavior of-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory toother_files
? The syntax for move, usually, ismv source directory
.
– Moving Man
1 hour ago
@MovingMan The{}
will replaced by the current pathname thatfind
is looking at.
– Kusalananda
1 hour ago
1
@MovingMan-exec
takes a utility and arguments. To know where that command line ends,find
looks for;
. The;
needs to be escaped to protect it from the shell.
– Kusalananda
59 mins ago
|
show 4 more comments
2
You'd better use-type f
or it will try to moveother_files
, and any other subdirectory ofdata
that we don't know about.
– Kusalananda
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
I'm trying to figure out the behavior of-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory toother_files
? The syntax for move, usually, ismv source directory
.
– Moving Man
1 hour ago
@MovingMan The{}
will replaced by the current pathname thatfind
is looking at.
– Kusalananda
1 hour ago
1
@MovingMan-exec
takes a utility and arguments. To know where that command line ends,find
looks for;
. The;
needs to be escaped to protect it from the shell.
– Kusalananda
59 mins ago
2
2
You'd better use
-type f
or it will try to move other_files
, and any other subdirectory of data
that we don't know about.– Kusalananda
1 hour ago
You'd better use
-type f
or it will try to move other_files
, and any other subdirectory of data
that we don't know about.– Kusalananda
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
@Kusalananda is correct, I'm going to need that.
– Moving Man
1 hour ago
I'm trying to figure out the behavior of
-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory to other_files
? The syntax for move, usually, is mv source directory
.– Moving Man
1 hour ago
I'm trying to figure out the behavior of
-exec mv {} /home/username/data/other_files/ ;
. How is it that this moves from the current directory to other_files
? The syntax for move, usually, is mv source directory
.– Moving Man
1 hour ago
@MovingMan The
{}
will replaced by the current pathname that find
is looking at.– Kusalananda
1 hour ago
@MovingMan The
{}
will replaced by the current pathname that find
is looking at.– Kusalananda
1 hour ago
1
1
@MovingMan
-exec
takes a utility and arguments. To know where that command line ends, find
looks for ;
. The ;
needs to be escaped to protect it from the shell.– Kusalananda
59 mins ago
@MovingMan
-exec
takes a utility and arguments. To know where that command line ends, find
looks for ;
. The ;
needs to be escaped to protect it from the shell.– Kusalananda
59 mins ago
|
show 4 more comments
The following line finds all files and hidden files in the current directory that are not *.txt
and not a path and move them into newpath:
ls -1p | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
The following is the same but moves also hidden files:
ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
Both command lines don't scan directories recursively and don't move directories
1
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
This isn't an issue for find (use-exec ... {}
or-print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)
– nohillside
1 min ago
add a comment |
The following line finds all files and hidden files in the current directory that are not *.txt
and not a path and move them into newpath:
ls -1p | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
The following is the same but moves also hidden files:
ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
Both command lines don't scan directories recursively and don't move directories
1
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
This isn't an issue for find (use-exec ... {}
or-print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)
– nohillside
1 min ago
add a comment |
The following line finds all files and hidden files in the current directory that are not *.txt
and not a path and move them into newpath:
ls -1p | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
The following is the same but moves also hidden files:
ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
Both command lines don't scan directories recursively and don't move directories
The following line finds all files and hidden files in the current directory that are not *.txt
and not a path and move them into newpath:
ls -1p | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
The following is the same but moves also hidden files:
ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs mv -vt newpath
Both command lines don't scan directories recursively and don't move directories
answered 18 mins ago
Sir Jo Black
1865
1865
1
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
This isn't an issue for find (use-exec ... {}
or-print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)
– nohillside
1 min ago
add a comment |
1
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
This isn't an issue for find (use-exec ... {}
or-print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)
– nohillside
1 min ago
1
1
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
This might have a problem with files whose names contain a space.
– nohillside
9 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
Is true. But also using find we've the same issue. Have you an indication to solve this issue without using scripts?.
– Sir Jo Black
3 mins ago
This isn't an issue for find (use
-exec ... {}
or -print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)– nohillside
1 min ago
This isn't an issue for find (use
-exec ... {}
or -print0 | xargs -0 ...
). The problem can't be solved for text/pipe based shell constructs though (at least not with reasonable effort and complexity)– nohillside
1 min ago
add a comment |
Moving Man is a new contributor. Be nice, and check out our Code of Conduct.
Moving Man is a new contributor. Be nice, and check out our Code of Conduct.
Moving Man is a new contributor. Be nice, and check out our Code of Conduct.
Moving Man 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.
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%2funix.stackexchange.com%2fquestions%2f492006%2fmove-all-files-not-ending-with-txt%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
so you would like to move all files which are not ending with
.txt
from currunt directory/home/username/data
to its sub-directory/home/username/data/other_files
'.... am i right?– msp9011
1 hour ago
find DIR ! -name '*.txt'
might help. Also can you add an example of source and target structure? Right now it's not clear whether the other directories beneath/home/username/data
need to be recreated beneath/home/username/data/other_files/
.– nohillside
1 hour ago
Related: unix.stackexchange.com/q/154818/315749 (You would just need to adapt it by negating the
-name
test with a!
and maybe adding a-type f
test to match only regular files).– fra-san
1 hour ago
@msp9011 You're correct.
– Moving Man
1 hour ago
@nohillside They don't need to be recreated within
other_files
because I only want to move files that are directly on the root ofhome/username/data
.– Moving Man
1 hour ago