Copy specific files from one folder to another in Unix/Linux - linux

I want to do a very similar thing than here. The difference is that I want to copy only the files containing specific string in their name (e.g. file_00).
Using the answer from this post, I tried this :
cp -a /home/folder_1/. find . -name "*file_00*" - print /home/folder_2
But the function cp doesn't recognize the function find. Then I tried
cp -a /home/yanncochet/folder_1/. -e'file_00' /home/yanncochet/folder_2
But same error message. Can anyone help ?

find is a separate program from cp. What I think you were trying to do was use find as input for cp. You can use command substitution for that:
cp $(find directory/ -name "*file_00*") destination/
$(...) basically runs the command inside and returns it's output.

Related

Simple Bash Script that recursively searches in subdirs for a certain string

i recently started learning linux because a ctf contest is coming in the next months. The problem that I struggle with is that i am trying to make a bash script that starts from a directory, checks if the content is a directory or other kind of file. If it is a file,image etc apply strings $f | grep -i 'abcdef', if it is a directory cd to that directory and start over. i have c++ experience and i understand the logic but i can't really make it work.I can't succesfully implement the loop that goes thru all the subdirectories. All help would be appreciated!
you don not need a loop for this implementation. The find command can do what you are looking after.
for instance:
find /home -type f -exec sh -c " strings {} | grep abcd " \;
explain:
/home is you base directory can be anything
-type f: means a regular file
-exec from the man page:
"Execute command; true if 0 status is returned. All
following arguments to find are taken to be arguments to
the command until an argument consisting of ;' is encountered. The string {}' is replaced by the current
file name being processed everywhere it occurs in the
arguments to the command, not just in arguments where it
is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `') or
quoted to protect them from expansion by the shell. See
the EXAMPLES section for examples of the use of the -exec
option. The specified command is run once for each
matched file. The command is executed in the starting
directory. There are unavoidable security problems
surrounding use of the -exec action; you should use the
-execdir option instead."
If you want to just find the string in a file and you do not HAVE TO first find a directory and then a file and then search, you can just simply find the text with grep.
Go to the the parent directory and execute :
grep -iR "abcd"
Or from any place,
grep -iR "abcd" /var/log/mylogs/
Suggesting a grep command on find filter results:
grep "abcd" $(find . -type f)

Shell Script to finding files in specific folder and based on "age" of files

hello i got homework to make a shell script in linux to find a file in specific folder based on the "age" of those files. and after that i want to move that file to other specific folder.
thank you before
One way is to use the find command, and specify the "age" with -mtime (or -newer if age relative to other files). See man find for more details.
To move the files you can use mv (again, see man mv).
Directories can be passed as arguments or stored in variables and then used
as variables in the commands.
Without knowing anything else about your assignment I'd say use something like this:
find <directory> -mtime <n> | xargs mv -t <destination>
where xargs is used to pass the results from find to the mv command.

parsing ls -R output into a variable in Unix

I am executing a ls-R /files/
I got the following output
./: nvision
./nvision: layout
./nvision/layout: abcd.txt
I am looking to get path in the listing like
/nvision
/nvision/layout/
/nvision/layout/abcd.txt
and I should be able to copy the required path to a variable
ps: I am not searching for nvision
I am trying to get the list of folders and files under files folder
can any one help me with that
Have you tried using find (see reference)
It would be as easy as find . to get the list of files and folders inside the current directory. Change the . to any path to obtain the list of files and directories inside that path:
nvision
nvision/abcd.txt
nvision/layout
To save it to a variable
var=`find .`
And to add the initial slash to every line (if required)
var=`find . -exec echo /{} \;`
Here var has no special meaning, it's just the variable name.
To later use the variable you can use $var or ${var}. For example, to print it or save it to file:
# Print the variable content
echo $var
# Save the content of var to a file
echo $var > /tmp/file.txt
You should really use find for these kind of things. Simply use find directory. If you require more specific output formatting you can make use of find's -printf option. Find is a really powerful tool that also allows all kinds of filtering. Make sure you check the documentation for more information: GNU FindUtils.
To store the results in a variable use one of the following statements:
result=`find ...`
or
result=$(find ...)
You can also use find to directly execute a command for each match using find's -exec option. Again, make sure to check out the documentation. It's really comprehensive.
Update (Mac / UNIX users – Linux users are not affected)
BSD find requires a path. Use
find .
instead of just
find
if you require a listing of all files in your working directory.
well the answer is all over this page you should be using find which lists all files found yo can define
where . is current folder otherwise replace . with path you are wishing to search
find .-type d -print
which lists directories only or find
or
find . -type f -print
which will list all files only
if you are looking for both then
find . -print
and if you only wish to define recursive level try
find . -maxdepth 1 -print
and here is a script
#!/bin/bash
for names in $(find . -type f -print); do
echo $names
done

linux find command output traversing

I need to create a shell script that will simply find files (e.g. *.jpg) under particular source (e.g. /var/www/html/folder1/source/) and have to make some operations with the output returned by the Find command. Below is the command i have written in my script
outputvar = find /var/www/html/folder1/source/ -name \*.jpg
How can i make a traverse operation on a variable that store the output of the find command?
You may want to put the output of your find command in a file, e.g.
find /var/www/html/folder1/source/ -name \*.jpg > /tmp/find.out
You could also put that output in a shell variable, e.g.
outfindvar=$(find /var/www/html/folder1/source/ -name \*.jpg)
and then you could iterate on them
for jpgfile in $outfindvar; do
## do something with $jpgfile
done
If you could have files with spaces in their name, be careful. In that case, consider using the -print0 action of find; or perhaps use an auxiliary script for the -exec action.
You really should read the Advanced Bash Scripting Guide
You may want to start your script with
#!/bin/bash -vx
while debugging it, and remove the -vx once your script is working well.
How about piping the output to whatever else it is you want to do with it?
find /var/www/html/folder1/source/ -name \*.jpg | other_command

Bash script cd issues

Hi all I have some problems with my script. I've read that changing the current directory from within a script is a bit of an issue. Basically I am looking for a single php file with a project folder and any sub-folders in it. And I want to change the directory to where that folder is and perform a command for it. So far no luck.
function findPHP(){
declare -a FILES
FILES=$(find ./ -name \*.php)
for file in "${FILES[#]}"
do
DIR=`dirname file`
( cd $DIR && doSomethingInThisDir &(...))
done
Any help would be greatly appreciated.
You are trying to iterate over FILES as an array, but it only has one element. In order to make the result of your subshell into an array, you can:
FILES=($(find ./ -name \*.php))
Note that it splits file names on spaces, so even though you properly quote below, it won't help. Alternatively, you could just let it split below (i.e. using your existing FILES) and use instead:
for file in $FILES
If you are using bash 4, you may want to have a look at recursive globbing... this would make it a bit easier:
for file in **/*.php
Note that you have to have the globstar shell option set, which you could enable with shopt -s globstar. This way is simpler and won't break on whitespace.
Also, you probably want $file here:
DIR=`dirname $file`
Or just use parameter expansion:
DIR=${file%/*}
There is no reason to use an array, or store the file list in anyway. If your find supports -execdir (eg gnufind 4.2.27), then use it. Otherwise, cd in a subshell as you have done:
#!/bin/bash
doSomethingInThisDir() ( cd $(dirname $1); ... )
export -f doSomethingInThisDir
find . -type f -exec bash -c 'doSomethingInThisDir {}' \;
I have defined the function using () instead of {}, but that is not necessary in this case. Normally, using () causes the function to run in a subshell, but that happens here anyway because find runs a separate process for each file.

Resources