Move all files NOT ending with .txt [duplicate]












10















This question already has an answer here:




  • Bash copy all files that don't match the given extensions

    3 answers




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?










share|improve this question









New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Jeff Schaller, αғsнιη, RalfFriedl, Wieland, JigglyNaga 2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • 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
    2 days 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
    2 days 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
    2 days ago










  • @msp9011 You're correct.
    – Moving Man
    2 days 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
    2 days ago
















10















This question already has an answer here:




  • Bash copy all files that don't match the given extensions

    3 answers




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?










share|improve this question









New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Jeff Schaller, αғsнιη, RalfFriedl, Wieland, JigglyNaga 2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • 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
    2 days 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
    2 days 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
    2 days ago










  • @msp9011 You're correct.
    – Moving Man
    2 days 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
    2 days ago














10












10








10


2






This question already has an answer here:




  • Bash copy all files that don't match the given extensions

    3 answers




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?










share|improve this question









New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












This question already has an answer here:




  • Bash copy all files that don't match the given extensions

    3 answers




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?





This question already has an answer here:




  • Bash copy all files that don't match the given extensions

    3 answers








bash grep find filenames xargs






share|improve this question









New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Jeff Schaller

39k1053125




39k1053125






New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Moving Man

566




566




New contributor




Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Moving Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by Jeff Schaller, αғsнιη, RalfFriedl, Wieland, JigglyNaga 2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, αғsнιη, RalfFriedl, Wieland, JigglyNaga 2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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
    2 days 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
    2 days 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
    2 days ago










  • @msp9011 You're correct.
    – Moving Man
    2 days 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
    2 days 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
    2 days 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
    2 days 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
    2 days ago










  • @msp9011 You're correct.
    – Moving Man
    2 days 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
    2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days ago












@msp9011 You're correct.
– Moving Man
2 days ago




@msp9011 You're correct.
– Moving Man
2 days 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
2 days 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
2 days ago










4 Answers
4






active

oldest

votes


















14














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 move 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`






share|improve this answer























  • In your second solution you're not accounting for *.txt, are you?
    – Moving Man
    2 days ago










  • @MovingMan Fixed it as you were typing your comment ;-)
    – Kusalananda
    2 days ago












  • Thank you very much.
    – Moving Man
    2 days ago










  • Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
    – Moving Man
    2 days 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
    2 days ago





















8














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






share|improve this answer



















  • 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
    2 days ago












  • @Kusalananda is correct, I'm going to need that.
    – Moving Man
    2 days 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
    2 days ago










  • @MovingMan The {} will replaced by the current pathname that find is looking at.
    – Kusalananda
    2 days 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
    2 days ago



















3














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 -maxdepth 1 -exec mv {} /home/username/data/other_files/ ;





share|improve this answer























  • Doesn‘t this also try to move the files put into other_files again?
    – nohillside
    2 days 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.
    2 days ago








  • 2




    -maxdepth 1 to stop it from walking down into subdirectories.
    – Kusalananda
    2 days ago










  • Kusalananda is correct.
    – Moving Man
    2 days ago










  • Can you please check my comment here?
    – Moving Man
    2 days ago



















2














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



If you have filenames that contains spaces you may use:



ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs -d'n' printf ""%s"n" | xargs mv -vt newpath






share|improve this answer



















  • 1




    This might have a problem with files whose names contain a space.
    – nohillside
    2 days 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
    2 days ago








  • 1




    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
    2 days ago






  • 1




    I think the edit I've added to my answer may solve this issue.
    – Sir Jo Black
    2 days ago






  • 1




    Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
    – nohillside
    2 days ago




















4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









14














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 move 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`






share|improve this answer























  • In your second solution you're not accounting for *.txt, are you?
    – Moving Man
    2 days ago










  • @MovingMan Fixed it as you were typing your comment ;-)
    – Kusalananda
    2 days ago












  • Thank you very much.
    – Moving Man
    2 days ago










  • Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
    – Moving Man
    2 days 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
    2 days ago


















14














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 move 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`






share|improve this answer























  • In your second solution you're not accounting for *.txt, are you?
    – Moving Man
    2 days ago










  • @MovingMan Fixed it as you were typing your comment ;-)
    – Kusalananda
    2 days ago












  • Thank you very much.
    – Moving Man
    2 days ago










  • Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
    – Moving Man
    2 days 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
    2 days ago
















14












14








14






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 move 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`






share|improve this answer














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 move 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`







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Kusalananda

122k16230375




122k16230375












  • In your second solution you're not accounting for *.txt, are you?
    – Moving Man
    2 days ago










  • @MovingMan Fixed it as you were typing your comment ;-)
    – Kusalananda
    2 days ago












  • Thank you very much.
    – Moving Man
    2 days ago










  • Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
    – Moving Man
    2 days 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
    2 days ago




















  • In your second solution you're not accounting for *.txt, are you?
    – Moving Man
    2 days ago










  • @MovingMan Fixed it as you were typing your comment ;-)
    – Kusalananda
    2 days ago












  • Thank you very much.
    – Moving Man
    2 days ago










  • Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
    – Moving Man
    2 days 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
    2 days ago


















In your second solution you're not accounting for *.txt, are you?
– Moving Man
2 days ago




In your second solution you're not accounting for *.txt, are you?
– Moving Man
2 days ago












@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
2 days ago






@MovingMan Fixed it as you were typing your comment ;-)
– Kusalananda
2 days ago














Thank you very much.
– Moving Man
2 days ago




Thank you very much.
– Moving Man
2 days ago












Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
– Moving Man
2 days ago




Side question, do you know why the regex I mentioned (find . | grep -E "./*.txt") failed?
– Moving Man
2 days 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
2 days 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
2 days ago















8














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






share|improve this answer



















  • 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
    2 days ago












  • @Kusalananda is correct, I'm going to need that.
    – Moving Man
    2 days 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
    2 days ago










  • @MovingMan The {} will replaced by the current pathname that find is looking at.
    – Kusalananda
    2 days 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
    2 days ago
















8














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






share|improve this answer



















  • 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
    2 days ago












  • @Kusalananda is correct, I'm going to need that.
    – Moving Man
    2 days 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
    2 days ago










  • @MovingMan The {} will replaced by the current pathname that find is looking at.
    – Kusalananda
    2 days 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
    2 days ago














8












8








8






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









nohillside

2,372919




2,372919








  • 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
    2 days ago












  • @Kusalananda is correct, I'm going to need that.
    – Moving Man
    2 days 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
    2 days ago










  • @MovingMan The {} will replaced by the current pathname that find is looking at.
    – Kusalananda
    2 days 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
    2 days ago














  • 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
    2 days ago












  • @Kusalananda is correct, I'm going to need that.
    – Moving Man
    2 days 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
    2 days ago










  • @MovingMan The {} will replaced by the current pathname that find is looking at.
    – Kusalananda
    2 days 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
    2 days 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
2 days 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
2 days ago














@Kusalananda is correct, I'm going to need that.
– Moving Man
2 days ago




@Kusalananda is correct, I'm going to need that.
– Moving Man
2 days 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
2 days 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
2 days ago












@MovingMan The {} will replaced by the current pathname that find is looking at.
– Kusalananda
2 days ago




@MovingMan The {} will replaced by the current pathname that find is looking at.
– Kusalananda
2 days 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
2 days 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
2 days ago











3














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 -maxdepth 1 -exec mv {} /home/username/data/other_files/ ;





share|improve this answer























  • Doesn‘t this also try to move the files put into other_files again?
    – nohillside
    2 days 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.
    2 days ago








  • 2




    -maxdepth 1 to stop it from walking down into subdirectories.
    – Kusalananda
    2 days ago










  • Kusalananda is correct.
    – Moving Man
    2 days ago










  • Can you please check my comment here?
    – Moving Man
    2 days ago
















3














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 -maxdepth 1 -exec mv {} /home/username/data/other_files/ ;





share|improve this answer























  • Doesn‘t this also try to move the files put into other_files again?
    – nohillside
    2 days 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.
    2 days ago








  • 2




    -maxdepth 1 to stop it from walking down into subdirectories.
    – Kusalananda
    2 days ago










  • Kusalananda is correct.
    – Moving Man
    2 days ago










  • Can you please check my comment here?
    – Moving Man
    2 days ago














3












3








3






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 -maxdepth 1 -exec mv {} /home/username/data/other_files/ ;





share|improve this answer














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 -maxdepth 1 -exec mv {} /home/username/data/other_files/ ;






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Tamas H.

785




785












  • Doesn‘t this also try to move the files put into other_files again?
    – nohillside
    2 days 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.
    2 days ago








  • 2




    -maxdepth 1 to stop it from walking down into subdirectories.
    – Kusalananda
    2 days ago










  • Kusalananda is correct.
    – Moving Man
    2 days ago










  • Can you please check my comment here?
    – Moving Man
    2 days ago


















  • Doesn‘t this also try to move the files put into other_files again?
    – nohillside
    2 days 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.
    2 days ago








  • 2




    -maxdepth 1 to stop it from walking down into subdirectories.
    – Kusalananda
    2 days ago










  • Kusalananda is correct.
    – Moving Man
    2 days ago










  • Can you please check my comment here?
    – Moving Man
    2 days ago
















Doesn‘t this also try to move the files put into other_files again?
– nohillside
2 days ago




Doesn‘t this also try to move the files put into other_files again?
– nohillside
2 days 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.
2 days 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.
2 days ago






2




2




-maxdepth 1 to stop it from walking down into subdirectories.
– Kusalananda
2 days ago




-maxdepth 1 to stop it from walking down into subdirectories.
– Kusalananda
2 days ago












Kusalananda is correct.
– Moving Man
2 days ago




Kusalananda is correct.
– Moving Man
2 days ago












Can you please check my comment here?
– Moving Man
2 days ago




Can you please check my comment here?
– Moving Man
2 days ago











2














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



If you have filenames that contains spaces you may use:



ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs -d'n' printf ""%s"n" | xargs mv -vt newpath






share|improve this answer



















  • 1




    This might have a problem with files whose names contain a space.
    – nohillside
    2 days 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
    2 days ago








  • 1




    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
    2 days ago






  • 1




    I think the edit I've added to my answer may solve this issue.
    – Sir Jo Black
    2 days ago






  • 1




    Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
    – nohillside
    2 days ago


















2














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



If you have filenames that contains spaces you may use:



ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs -d'n' printf ""%s"n" | xargs mv -vt newpath






share|improve this answer



















  • 1




    This might have a problem with files whose names contain a space.
    – nohillside
    2 days 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
    2 days ago








  • 1




    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
    2 days ago






  • 1




    I think the edit I've added to my answer may solve this issue.
    – Sir Jo Black
    2 days ago






  • 1




    Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
    – nohillside
    2 days ago
















2












2








2






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



If you have filenames that contains spaces you may use:



ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs -d'n' printf ""%s"n" | xargs mv -vt newpath






share|improve this answer














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



If you have filenames that contains spaces you may use:



ls -1ap | grep -v "^.*.txt$" | grep -v ".*/$" | xargs -d'n' printf ""%s"n" | xargs mv -vt newpath







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Sir Jo Black

1965




1965








  • 1




    This might have a problem with files whose names contain a space.
    – nohillside
    2 days 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
    2 days ago








  • 1




    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
    2 days ago






  • 1




    I think the edit I've added to my answer may solve this issue.
    – Sir Jo Black
    2 days ago






  • 1




    Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
    – nohillside
    2 days ago
















  • 1




    This might have a problem with files whose names contain a space.
    – nohillside
    2 days 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
    2 days ago








  • 1




    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
    2 days ago






  • 1




    I think the edit I've added to my answer may solve this issue.
    – Sir Jo Black
    2 days ago






  • 1




    Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
    – nohillside
    2 days ago










1




1




This might have a problem with files whose names contain a space.
– nohillside
2 days ago




This might have a problem with files whose names contain a space.
– nohillside
2 days 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
2 days 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
2 days ago






1




1




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
2 days 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
2 days ago




1




1




I think the edit I've added to my answer may solve this issue.
– Sir Jo Black
2 days ago




I think the edit I've added to my answer may solve this issue.
– Sir Jo Black
2 days ago




1




1




Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
– nohillside
2 days ago






Nice. Did you try with file names containing n as well? Sorry to be a nuisance here, but there are so many special cases to cover that it's usually just not worth the effort. It might be an interesting intellectual challenge though :-)
– nohillside
2 days ago





Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy