scp download multiple files from multiple directories [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
first time I am using linux for work. I am trying to figure out how I can download multiple files with the same extension in multiple directories using scp.
For example:
/server/directoryA/directoryA1/nameA.txt
/server/directoryA/directoryA2/contactA.txt
/server/directoryA/directoryB1/nameB.txt
/server/directoryB/directoryB2/contactB.txt
I want to download all the *.txt file in one scp command. I can't seem to get it working.
I tried something like:
scp user#server:/server/*/*/*.txt .
I tried with -r too but doesn't seem to be working. Anyone can point me to the right command syntax? Thank you!

You can do this using ssh instead of scp:
ssh user#server 'find /server/ -name "*.txt" -print0 | xargs -0 tar -cO' | tar -xivf - -C .
this will copy all the *.txt into the current dir ".", but it will copy the directory structure too, so if you want just the txt files without the directory structure you will need to move all the downloaded txt files into the current directory by:
find -name "*.txt" -print0 | xargs -0 -I {} cp {} .
and then remove the empty directory structure:
rm ./server -r

Related

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.

finding files and moving their folders [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 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

chmod exclusions [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I know I should assign a group and then set an umask so that groups writable permissions persist but for whatever reason I can't do this. I need to chmod recursively a directory except one sub folder (web10), would the following work?
cd /var/www/clients/
find . -type f -not -path "*web10*" -exec chmod 777 '{}' \;
If you want to exclude files or directories, you use -prune
find /var/www/clients/ -name web10 -type d -prune -o -type f -print0 | xargs -0 chmod 0640
You should also use xargs where possible. With -exec you call the command once for every file found, whereas xargs collects as many files as possible and calls the command once for N files, resulting in a more efficient execution and better performance.

List of All Folders and Sub-folders [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In Linux, I want to find out all Folder/Sub-folder name and redirect to text file
I tried ls -alR > list.txt, but it gives all files+folders
You can use find
find . -type d > output.txt
or tree
tree -d > output.txt
tree, If not installed on your system.
If you are using ubuntu
sudo apt-get install tree
If you are using mac os.
brew install tree
find . -type d > list.txt
Will list all directories and subdirectories under the current path. If you want to list all of the directories under a path other than the current one, change the . to that other path.
If you want to exclude certain directories, you can filter them out with a negative condition:
find . -type d ! -name "~snapshot" > list.txt
As well as find listed in other answers, better shells allow both recurvsive globs and filtering of glob matches, so in zsh for example...
ls -lad **/*(/)
...lists all directories while keeping all the "-l" details that you want, which you'd otherwise need to recreate using something like...
find . -type d -exec ls -ld {} \;
(not quite as easy as the other answers suggest)
The benefit of find is that it's more independent of the shell - more portable, even for system() calls from within a C/C++ program etc..

How can I use the `find` command in Linux to remove non-empty directories? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have temp directories full of junk that all start with __temp__ (e.g. __temp__user_uploads), which I want to delete with a cleanup function. My function attempt is to run:
find . -name __temp__* -exec rm -rf '{}' \;
If I run the command and there are multiple __temp__ directories (__temp__foo and __temp__bar), I get the output:
find: __temp__foo: unknown option
If I run the command and there is only 1 __temp__ directory (__temp__foo), it is deleted and I get the output:
find: ./__temp__foo: No such file or directory
Why doesn't the command work, why is it inconsistent like that, and how can I fix it?
Use a depth-first search and quote (or escape) the shell metacharacter *:
find . -depth -name '__temp__*' -exec rm -rf '{}' \;
Explanation
Without the -depth flag, your find command will remove matching filenames and then try to descend into the (now unlinked) directories. That's the origin of the "No such file or directory" in your single __temp__ directory case.
Without quoting or escaping the *, the shell will expand that pattern, matching several __temp__whatever filenames in the current working directory. This expansion will confuse find, which is expecting options rather than filenames at that point in its argument list.

Resources