finding files and moving their folders [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have a huge number of text files, organized in a big folder tree, on Debian Linux. What I need is to find all text files having a specific name pattern and then move the containing folder to a destination.
Example:
/home/spenx/src/a12/a1a22.txt
/home/spenx/src/a12/a1a51.txt
/home/spenx/src/a12/a1b61.txt
/home/spenx/src/a12/a1x71.txt
/home/spenx/src/a167/a1a22.txt
/home/spenx/src/a167/a1a51.txt
/home/spenx/src/a167/a1b61.txt
/home/spenx/src/a167/a1x71.txt
The commands:
find /home/spenx/src -name "a1a2*txt"
mv /home/spenx/src/a12 /home/spenx/dst
mv /home/spenx/src/a167 /home/spenx/dst
The result:
/home/spenx/dst/a12/a1a22.txt
/home/spenx/dst/a167/a1a22.txt
Thank you for your help.
SK

combination of find, dirname and mv along with xargs should solve your problem
find /home/spenx/src -name "a1a2*txt" | xargs -n 1 dirname | xargs -I list mv list /home/spenx/dst/
find will fetch list of files
dirname will extract path of file. Note that it can only take one argument at a time
mv will move source directories to destination
xargs is the key to allow output of one command to be passed as arguments to next command
For details of options used with xargs, refer to its man page of just do man xargs on terminal

You can execute:
find /home/spenx/src name "a1a2*txt" -exec mv {} /home/spenx/dst \;
Font: http://www.cyberciti.biz/tips/howto-linux-unix-find-move-all-mp3-file.html

Create this mv.sh script in the current directory that will contain this:
o=$1
d=$(dirname $o)
mkdir /home/spenx/dst/$d 2>/dev/null
mv $o /home/spenx/dst/$d
Make sure it is executable by this command:
chmod +x mv.sh
Next call this command:
find /home/spenx/src -name "a1a2*txt" -exec ./mv.sh {} \;

find /home/spenx/src -name "a1a2*txt" -exec mv "{}" yourdest_folder \;

There's probably multiple ways to do this, but, since it seems you might have multiple matches in a single directory, I would probably do something along this line:
find /home/spenx/src -name "a1a2*txt" -print0 | xargs -0 -n 1 dirname | sort -u |
while read d
do
mv "${d}" /home/spenx/dst
done
It's kind of long, but the steps are:
Find the list of all matching files (the find part), using -print0 to compensate for any names that have spaces or other odd characters in them
extract the directory part of each file name (the xargs ... dirname part)
sort and uniquify the list to get rid of duplicates
Feed the resulting list into a loop that moves each directory in turn

Related

Linux find specific data to a file and entire system [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to find a specific data from a file. And I want to search it from entire system of my Linux. Is this possible?
The above answer will work but that will try grepping directories as well for the pattern which inturn will throw an error. The best solution will be to search for files only. This will considerably reduce the search time as well.
find / -type f -exec grep -i <pattern> {} \;
If you are only interested in listing the files containing the pattern, you could pass the -l switch in grep.
find / -type f -exec grep -il <pattern> {} \;
If you would like to list both the file(s) and the pattern, you can pass the -H switch in grep.
find / -type f -exec grep -iH <pattern> {} \;
#alvits - Thanks for the suggestion.
find / -name "*" -exec grep -q <pattern> '{}' \; -print
This command search from root directory(/); and so all sub directories.
You can replace your search pattern in place of in command.
And it will print all files contains your pattern.
If you know the file extension of your searching file, you can limit search by replacing * with *.extention within command.

find *.tar then extract and delete the files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to find a tar file, extract the files then remove all the extracted files - I'm able to perform the find and extraction or find the file and remove it but I'm not able to string all three together.
Here is my best attempt below. It runs without error but doesn't delete the extracted files so I'm stuck on how to remove the files I've extracted to the current directory.
find ~ -name '*.tar' | xargs tar -xf && rm -f
I tried extracting the tar to another directory then removing the directory but couldn't get it to work while using xargs. I've tried searching quite a few different areas but couldn't find anything so I appreciate the help.
The && ends the pipeline, it's not part of the xargs command.
You can just run the commands using the -exec option to find:
find ~ -name '*.tar' -exec tar -xf {} \; -exec rm -f {} \;
To run two or multiple commands with xargs:
find ~ -name '*.tar' | xargs -I {} sh -c 'tar -xf {} && rm -f {}'
Only after successfully unpacking the tar file is deleted.

Linux "mv" command, created a new file (when no folder is available) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I am using Mac terminal, inside using linux commands to find list of ".jpg" files and try to move to "myimages" folder in current directory with below command.
find . -name "*.jpg" -type f -exec mv {} myimages \;
At the time of executing above command I don't have a folder named "myimages" .
Now all my .jpg files are disappeared and there is a new file "myimages" is created in the current directory.
i guess all my images are converted into single file with name "myimages".
how to get back all images back from "myimgages" file?
Please help me.
Your images are gone. Each image gets moved to the file myimages, effectively overwriting the existing file. You can try with recovery software to get them back.
In the future, use the command find . -name "*.jpg" -type f -exec mv {} myimages/ \; instead (note the trailing slash). You can also provide the -i flag to mv, so it prompts before overwriting existing files.
Your photos are gone. Your command renamed all of them, one by one, to myimages.
You can:
Try to recover from backups
Try PhotoRec, an image recovery tool
Try Exif Untrasher, an image recovery tool
Try some other image recovery tool
Btw, you cannot run "linux commands" on a Mac. On a Mac you are running Mac commands (compiled for Mac OS X), or shell scripts. Also the ubuntu tag didn't make any sense for your question.
In the future you can avoid such mistakes by putting a trailing slash at the end of the destination if it's supposed to be a directory:
find . -name "*.jpg" -type f -exec mv {} myimages/ \;
This way you would get an error message saying:
mv: rename blah to myimages/: No such file or directory
and nothing would be moved, and you would still have your fields.
Restore them from backup.
The commands you executed are
mv file1 myimages # rename file
mv file2 myimages # rename file, discarding the old file previously referred to by myimages
and so on.
Besides the trailing slash mentioned by knittl, I'd recommend for the next time to use
find . -name "*.jpg" -type f -exec mv -t myimages/ {} +
which clearly states "doubly" that myimages is the target directory and which allows for multiple files to be moved at one mv execution.
how to get back all images back from "myimgages" file?
You can't. Since the target directory myimages did not exist, every output from find resulted in something like:
mv ./file1.jpg myimages
mv ./file2.jpg myimages
...
In each of these commands, the target was myimages. Since it didn't exist as a directory, mv assumed that it was the target filename.
You could fix the command by saying:
find . -name "*.jpg" -type f -exec mv -t myimages {} \;

How to show a 'grep' result with the complete path or file name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 10 months ago.
Improve this question
How can I get the complete file path when I use grep?
I use commands like
cat *.log | grep somethingtosearch
I need to show the result with the complete file path from where the matched result were taken out.
How can I do it?
Assuming you have two log-files in:
C:/temp/my.log
C:/temp/alsoMy.log
'cd' to C: and use:
grep -r somethingtosearch temp/*.log
It will give you a list like:
temp/my.log:somethingtosearch
temp/alsoMy.log:somethingtosearch1
temp/alsoMy.log:somethingtosearch2
I think the real solution is:
cat *.log | grep -H somethingtosearch
Command:
grep -rl --include="*.js" "searchString" ${PWD}
Returned output:
/root/test/bas.js
If you want to see the full paths, I would recommend to cd to the top directory (of your drive if using Windows)
cd C:\
grep -r somethingtosearch C:\Users\Ozzesh\temp
Or on Linux:
cd /
grep -r somethingtosearch ~/temp
If you really resist on your file name filtering (*.log) and you want recursive (files are not all in the same directory), combining find and grep is the most flexible way:
cd /
find ~/temp -iname '*.log' -type f -exec grep somethingtosearch '{}' \;
It is similar to BVB Media's answer.
grep -rnw 'blablabla' `pwd`
It works fine on my Ubuntu 16.04 (Xenial Xerus) Bash.
For me
grep -b "searchsomething" *.log
worked as I wanted
This works when searching files in all directories.
sudo ls -R | grep -i something_bla_bla
The output shows all files and directories which include "something_bla_bla". The directories with path, but not the files.
Then use locate on the wanted file.
The easiest way to print full paths is to replace the relative start path with the absolute path:
grep -r --include="*.sh" "pattern" ${PWD}
Use:
grep somethingtosearch *.log
and the filenames will be printed out along with the matches.

Find all files with name containing string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I have been searching for a command that will return files from the current directory which contain a string in the filename. I have seen locate and find commands that can find files beginning with something first_word* or ending with something *.jpg.
How can I return a list of files which contain a string in the filename?
For example, if 2012-06-04-touch-multiple-files-in-linux.markdown was a file in the current directory.
How could I return this file and others containing the string touch? Using a command such as find '/touch/'
Use find:
find . -maxdepth 1 -name "*string*" -print
It will find all files in the current directory (delete maxdepth 1 if you want it recursive) containing "string" and will print it on the screen.
If you want to avoid file containing ':', you can type:
find . -maxdepth 1 -name "*string*" ! -name "*:*" -print
If you want to use grep (but I think it's not necessary as far as you don't want to check file content) you can use:
ls | grep touch
But, I repeat, find is a better and cleaner solution for your task.
Use grep as follows:
grep -R "touch" .
-R means recurse. If you would rather not go into the subdirectories, then skip it.
-i means "ignore case". You might find this worth a try as well.
The -maxdepth option should be before the -name option, like below.,
find . -maxdepth 1 -name "string" -print
find $HOME -name "hello.c" -print
This will search the whole $HOME (i.e. /home/username/) system for any files named “hello.c” and display their pathnames:
/Users/user/Downloads/hello.c
/Users/user/hello.c
However, it will not match HELLO.C or HellO.C. To match is case insensitive pass the -iname option as follows:
find $HOME -iname "hello.c" -print
Sample outputs:
/Users/user/Downloads/hello.c
/Users/user/Downloads/Y/Hello.C
/Users/user/Downloads/Z/HELLO.c
/Users/user/hello.c
Pass the -type f option to only search for files:
find /dir/to/search -type f -iname "fooBar.conf.sample" -print
find $HOME -type f -iname "fooBar.conf.sample" -print
The -iname works either on GNU or BSD (including OS X) version find command. If your version of find command does not supports -iname, try the following syntax using grep command:
find $HOME | grep -i "hello.c"
find $HOME -name "*" -print | grep -i "hello.c"
OR try
find $HOME -name '[hH][eE][lL][lL][oO].[cC]' -print
Sample outputs:
/Users/user/Downloads/Z/HELLO.C
/Users/user/Downloads/Z/HEllO.c
/Users/user/Downloads/hello.c
/Users/user/hello.c
If the string is at the beginning of the name, you can do this
$ compgen -f .bash
.bashrc
.bash_profile
.bash_prompt
An alternative to the many solutions already provided is making use of the glob **. When you use bash with the option globstar (shopt -s globstar) or you make use of zsh, you can just use the glob ** for this.
**/bar
does a recursive directory search for files named bar (potentially including the file bar in the current directory). Remark that this cannot be combined with other forms of globbing within the same path segment; in that case, the * operators revert to their usual effect.
Note that there is a subtle difference between zsh and bash here. While bash will traverse soft-links to directories, zsh will not. For this you have to use the glob ***/ in zsh.
find / -exec grep -lR "{test-string}" {} \;
grep -R "somestring" | cut -d ":" -f 1

Resources