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?
.txt
from currunt directory/home/username/data
to its sub-directory/home/username/data/other_files
'.... am i right? – Siva Jan 02 '19 at 10:40find 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 Jan 02 '19 at 10:41other_files
because I only want to move files that are directly on the root ofhome/username/data
. – Moving Man Jan 02 '19 at 11:00find -exec xyz
to do an action on a lot of files, change it to find-exec echo xyz
so you can see what commands are being generated. You can pipe this to a file to inspect by eye, or tomore
, and when happy you can run said file. – Ben Jan 02 '19 at 17:40