date int64 1,220B 1,719B | question_description stringlengths 28 29.9k | accepted_answer stringlengths 12 26.4k | question_title stringlengths 14 159 |
|---|---|---|---|
1,321,630,566,000 |
Prepend user-defined string to all files and folders recursively using find and rename.
I’d like to prepend “x “ (no quotes) to a directory and all of its contents down through all subdirectories. I’m a beginner using macOS Mojave 10.14.6 and Terminal. I downloaded rename using Homebrew for this purpose.
Example:
/Old... |
Using gnu tools:
First install GNU find via
brew install findutils
Then:
gfind . -depth -exec rename -n 's@(?<=/)[\s\w\.-]+$@x $&@' {} \;
With perl rename.
Remove -n switch when the output looks good.
Note
-depth here is very important, it traverse first files from directories before renaming directories themselves... | Prepend user-defined string to all files and folders recursively using find and rename |
1,321,630,566,000 |
So wget has an ability to recursively download files, however it does it one file at a time.
I would like to pass in a directory URL, and for each URL it encounters in the recursion for it to spawn off a downloading process.
One way I was thinking to do this is to somehow use wget to print out the URLs it encounters, ... |
I've been ruminating on this, and I'm not convinced wget is the best tool for the job here.
Here is how I would do this in the year 2022, using a tool like pup that is specifically designed to parse HTML (in pup's case, with CSS selectors):
wget -q -O- https://ubuntu.com/download/alternative-downloads \
| pup 'a[hre... | How can I use wget to create a list of URLs from an index.html? |
1,321,630,566,000 |
I'm trying to create a graph of the distribution of file sizes on my ext4 system. I'm trying to write a script to scrape this information from my computer somehow. I don't care where the files are stored in the directory structure, only how much space each takes up. I know file sizes are stored in the inode metadata, ... |
If you want a C API, you're going to end up with GNU nftw, the GNU file tree walk. DON'T fool yourself into using plain old ftw, you will get inaccurate data. You'll need to write a "per file" function that uses the struct stat that nftw passes into the "per file" function. You can have the "per file" function put f... | Fastest way to get list of all file sizes |
1,322,691,968,000 |
I was told by my friend that one can work with -r switch to find files recursively in directories and subdirectories.Please tell me the error in the given statement it does not work
find / -type f -r -name "abc.txt"
|
The reason it doesn't work is because find has no -r option. While it is true that for many programs the -r flag means 'recursive', this is not the case for all and it is not the case for find. The job of find is to search for files and directories, it is not very often that you don't want it to be recursive.
You can ... | find command in linux |
1,322,691,968,000 |
I wrote the following script for finding the number of pdf and tex files from the current directory, including the subdirectories and hidden files. The following code is able to find the number of pdf files upto 2 levels of subdirectories below, but after that it tells that there are no sub directories....
#!/bin/bash... |
find works fine to me:
$ find . -name '*.pdf' -o -name '*.tex' | wc -l
75
$ find . -name '*.pdf' | wc -l
16
$ find . -name '*.tex' | wc -l
59
$ echo $((16+59))
75
Edit:
To handle special case: newline in filename
$ find . \( -name '*.pdf' -o -name '*.tex' \) -printf x | wc -c
| Script to count files matching a pattern in subdirectories |
1,322,691,968,000 |
Possible Duplicate:
Searching for string in files
Suppose I have a directory called Home and it is my current directory.
And in this home directory I have many other directories
directory1, directory2, etc.
How do I do a grep to find the occurrence of a word (say "AXN") in any of the files in all of these subdire... |
You can use something like:
grep -r "AXN" .
Use -ir if you want it to be case-insensitive.
| How to search all subdirectories and their subdirectories for the occurence of a word using grep? [duplicate] |
1,322,691,968,000 |
Think of it as going to the most high level folder, doing a Ctrl Find, and searching .DS_Store and deleting them all.
I want them all deleted, from all subfolders and subfolders subfolders and so on. Basically inside the top level folder, there should be no .DS_Store file anywhere, not even in any of its subfolders.
W... |
find top-folder -type f -name '.DS_Store' -exec rm -f {} +
or, more simply,
find top-folder -type f -name '.DS_Store' -delete
where top-folder is the path to the top folder you'd like to look under.
To print out the paths of the found files before they get deleted:
find top-folder -type f -name '.DS_Store' -print -e... | How to remove all occurrences of .DS_Store in a folder |
1,322,691,968,000 |
I'm new to the gzip command, so I googled some commands to run so I can gzip an entire directory recursively. Now while it did that, it converted each of my files to the gzip format and added .gz to the end of each of their filenames.. is there a way to ungzip them all, one by one?
|
There are essentially two options for going through the whole directory tree:
Either you can use find(1):
find . -name '*.gz' -exec gzip -d "{}" \;
or if your shell has recursive globbing you could do something like:
for file in **/*.gz; do gzip -d "$file"; done
| I accidentally GZIPed a whole bunch of files, one by one, instead of using tar(1). How can I undo this mess? |
1,322,691,968,000 |
I have several directories through which I want to recursively iterate over and change the file name of
*.GEOT14246.*
to
*.GEOT15000.*
Is there a one liner on the bash script to do this or do I have to the write a script with a for loop. My try (which doesn't work) is below:
#!/bin/bash
for file in `find . -type... |
This should work:
find . -type f -name "*.GEOT14246.*" -print0 | \
xargs -0 rename 's/GEOT14246/GEOT15000/'
given there is not directories named *.GEOT14246.*
A bash variant using find could be something like:
while IFS= read -r -d $'\0' file; do
printf "MV: %-40s => %s\n" "$file" "${file/GEOT14246/GEOT15000}"
... | How to rename pattern in one file to another over several directories recursively |
1,322,691,968,000 |
Suppose I have a directory structure like this:
projects/
project1/
src/
node_modules/
dir1/
dir2/
dir3/
file
project2/
node_modules/
dir4/
Starting from projects/ I want to delete the contents of all node_modules/ directories, but I do not want to delete the node_modules... |
The following will delete all files and directories within a path matching node_modules:
find . -path '*/node_modules/*' -delete
If you would like to check what will be deleted first, then omit the -delete action.
| How to recursively delete the contents of all "node_modules" directories (or any dir), starting from current directory, leaving an empty folder? |
1,322,691,968,000 |
I have made a mistake and dumped files together into the same directory. Luckily I can sort them based on the filename:
'''
2019-02-19 20.18.58.ndpi_2_2688_2240.jpg
'''
Where the # bit or 2 in this specific case is the location information, as it were. The range is 0-9 and the filenames are all identical length so tha... |
The shell is splitting input on whitespace. You can use bash globbing with the recursive ** to get the filenames split properly:
shopt -s globstar
for f in **/*_0_*; do mv "$f" /dest/; done
If they're all going to the same place, the loop is not needed:
shopt -s globstar
mv **/*_0_* /dest/
... or use find -exec, w... | Sort and mv files based on filename (with spaces), recursively |
1,322,691,968,000 |
I want to recursively delete all files that end with .in. This is taking a long time, and I have many cores available, so I would like to parallelize this process. From this thread, it looks like it's possible to use xargs or make to parallelize find. Is this application of find possible to parallelize?
Here is my cur... |
Replacing -delete with -print (which is the default) and piping
into GNU parallel should mostly do it:
find . -name '*.in' -type f | parallel rm --
This will run one job per core; use -j N to use N parallel jobs
instead.
It's not completely obvious that this will run much faster than
deleting in sequence, since delet... | Parallelize recursive deletion with find |
1,322,691,968,000 |
I think its a simple question, but the answer is probably a little more complicated :P
Edit: Actually, its not complicated at all!^^
So I have a directory with multiple svn projects and I would like to search through all recent files (in trunk folder) by content in all projects.
Here is somewhat the folders look like:... |
As suggested in comments above:
grep -l some-pattern ./Projects/*/trunk/*
or recursively if there are subdirs under each trunk (and your grep supports -r):
grep -lr some-pattern ./Projects/*/trunk/
| Search for files by content only in trunk subdirectories |
1,322,691,968,000 |
Situation
I have a file where each line has an IP address and I want to see if these Ip's are found in access.log
File name: IpAddressess
Contents example :
192.168.0.1
192.168.0.2
192.168.1.5
etc etc
Now I want to scan access.log for these IP addresses contained in the file IpAddressess
Can I use the command grep fo... |
You are looking for the -f option, described in man grep on my Arch Linux system as:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. If this option is
used multiple times or is combined with the -e (--regexp)
option, search for all patterns given. The empty fi... | How to provide grep a file with ip addresses to look for in access.log |
1,322,691,968,000 |
I'm trying to recursively delete a directory with rm -rf, but this fails because some inner directories don't have the w permission set. I know how to fix this with chmod. However, this requires to iterate over the whole directory twice, which can be slow.
Is there a way to remove such a directory in one go? (Assuming... |
rsync with an empty dummy directory seems fine:
mkdir empty; rsync -r --delete empty/ targetdir/; rmdir empty targetdir
With a 10x repeated test on a simple example, this took 10-14s (14 was an outlier, all others took 10 or 11s),
vs. chmod -R u+w targetdir && rm -rf targetdir, which took 19-25s
and find targetdir -ty... | rm -rf with missing w permissions on directories without root or chmod |
1,322,691,968,000 |
I have an TI DaVinci-based (ARM architecture kin to OMAP) system which netboots using TFTP and NFS-mounted root filesystem, and am trying to make it boot standalone without a netboot server.
The basic approach is to copy the kernel image to the NAND flash and the root filesystem to a connected SATA disk (NAND flash is... |
BusyBox's find supports the -xdev option, so you can make a cpio archive of the root filesystem that way. Unlike tar, cpio does not archive a directory's contents when it archives that directory.
find . -xdev | cpio -H newc -o |
{ cd /mnt && cpio -m -i; }
I don't quite understand why you're building the image from a ... | clone root directory tree using busybox |
1,322,691,968,000 |
I use this command to make a montage of all the images in a directory:
gmic *jpg -gimp_montage 4,\""V(H(0,1),H(2,V(3,4)))"\",1,1.0,0,5,0,0,0,255,0,0,0,0 -o -0000."$(date)".jpg
I want to run this command in a directory and its subdirectories recursively. So in each directory I would create a montage of the images in t... |
Ok, so you want to run a command in each directory in a directory tree — the current directory, its subdirectories, their subdirectories, etc. The first thing to do is enumerate the directories in question. With the find command, tell it to list only directories:
find . -type d
The command you want to run in each dir... | Run a command with wildcards in each subdirectory |
1,322,691,968,000 |
I am interested to know if a single command line that would allow me to recursively copy a folder to all of our NGINX Virtual Host htdocs folders:
I need to copy that folder to all hosts located in vhosts :
/var/www/vhosts/*/htdocs/
|
With all due respect, I don't think the above code/answer is correct.
if [ -d dir] is probably an attempt to if [[ -d "$dir" ]].. or [[ -d "$dir" ]];..
The following code should work and do what you want.
vhostdirs=( ./var/www/vhosts/* )
for dir in "$vhostdirs"
do
cp -r "folder_to_be_copied" "$dir/htdoc... | Copy a folder and its content to all Nginx vhosts host |
1,322,691,968,000 |
The Issue
I was using python-skydrive to download files to my PC, and I accidentally corrupted a good amount of my PDF files. When I try to view them in Document Viewer, I get the following error message:
File type plain text document (text/plain) is not supported
$file ny.pdf
$ny.pdf
My Request
I'm looking for a... |
After investigation (see the comments in the question), it appeared that the "corrupted" files were in fact empty. This can happen when a downloading program create the entries in the filesystem but fails before having downloaded their content.
To look for them in the current directory and its subdirectories and move ... | Recursively find and move corrupted PDFs |
1,322,691,968,000 |
I am having to do chmod -R 755 some_dir where 'some_dir' contains '.git' folders. Is there any way I can exclude hidden files and folder when doing recursive chmod?
Note: chmoding .git folder is throwing the following error
some_dir/.git/objects/pack/pack-dd149b11c4e5d205e3022836d49a972684de8daa.idx': Operation not pe... |
Not with chmod alone. You'll need to use find:
find some_dir -name '.?*' -prune -o -exec chmod 755 {} +
Or with zsh (or ksh93 -G, or with tcsh after set globstar) globbing:
chmod 755 -- some_dir some_dir/**/*
(you can also do that with fish or bash -O globstar, but beware that bash versions prior to 4.3 and fish fol... | How to exclude hidden files in recursive chmod? |
1,322,691,968,000 |
From the chown manpage:
The following options modify how a hierarchy is traversed when the -R option is also specified. If more than one is specified, only the final one takes effect.
-H if a command line argument is a symbolic link to a directory, traverse it
-L traverse every symbolic link to a directory ... |
Your understanding is correct; these options match the same options in find.
Thus
chown -R .
or
chown -R -P .
changes the owner recursively without de-referencing any symlinks;
chown -R -H *
changes the owner recursively, de-referencing any symlinks in the current directory (since they end up being part of the argu... | What is the difference between -H and -L options of chown? |
1,322,691,968,000 |
I have the following directory tree:
records/13/2014.12.16/00/05.mpg
records/13/2014.12.16/00/15.mpg
records/13/2014.12.16/01/05.mpg
records/13/2014.12.16/02/15.mpg
records/15/2014.12.14/05/25.mpg
etc.
I need to rename every file which has *5.mpg in its name to *0.mpg. So for example:
mv records/13/2014.12.16/00/05.m... |
Just loop through all *.5mpg files and use help of parameter expansion to change filenames:
for file in *5.mpg; do mv -- "$file" "${file%5.mpg}"0.mpg; done
To do it for different directories set globstar option (shopt -s globstar in bash) and additionally take path component with dirname command or again using parame... | Moving files recursively if certain condition is met |
1,322,691,968,000 |
I'm trying to get a site with wget. The problem is that it:
Have user friendly name for the pages
http://domain/wiki/Section/Home,
http://domain/wiki/Section/Not+Home
http://domain/wiki/Section/Other+page
For some pages it uses query strings:
http://domain/wiki/Section/Home?one=value&other=value
and for some reas... |
You can use the [] notation to specify ranges of numbers and letters. Repeat for multiple.
*[0-9],*[0-9][0-9],*[0-9][0-9][0-9]
|____||__________||_______________|
| | |
| | +---------- Reject ending with 000 to 999
| +------------------------- Reject ending with 00 ... | wget recursive with files without extension |
1,322,691,968,000 |
I'm migrating a directory of files containing fairly simple ASP code over to a PHP server and I need to modify the contents of all the files with a find and replace mechanism. I'm not great with regular expressions, but I've used this to change a few things already:
find . -name "*.php" -print0 | xargs -0 -n 1 sed -i ... |
There are various ways of doing this, I would recommend taking advantage of Perl's quotemeta function.
First, make a tab separated text file containing the search patterns in the first column and their replacement in the second:
$ cat pats.txt
<% if request("page") = "" then %> <?php if(!isset($_... | Change some ASP code to PHP code in all files |
1,322,691,968,000 |
I need to recursively iterate through all the subdirectories of a folder.
Within the subdirectories, if there's a file with an extension '.xyz' then I need to run a specific command in that folder once.
Here's what I have so far
recursive() {
for d in *; do
if [ -d "$d" ]; then
(cd -- "$d" && recursive)
... |
find has a builtin flag to print strings, which is pretty useful here:
find -iname "*.xyz" -printf "%h\n" prints the names of all directories that contain a file that matches your pattern (the %h is just find's magic syntax that expands to the file directory and \n is, of course, a linebreak).
Therefore, this does w... | Recursively iterate through all subdirectories, If a file with a specific extension exists then run a command in that folder once |
1,322,691,968,000 |
I use this commandline to convert jpegs to pdfs inside a folder.
for f in *.jpg; do echo "Converting $f"; convert "$f" "$(basename "$f" .jpg).pdf"; done
However, I would like to convert all .jpg from several folders to .pdf located inside the samefolder. I need a bash command which says "go inside folder A, launch t... |
Updated to allow file names *.jpg and *.JPG according to updated question:
Use -iname for case insensitive comparison, and variable expansion with pattern ${file%.[jJ][pP][gG]}. This will actually match .jpg in any capitalization, e.g. .JpG.
Assuming Folder A and Folder B are in the current directory and you want to d... | Recursive conversion of jpeg files |
1,322,691,968,000 |
I am experiencing an issue with a indexing program running much longer than expected. I want to rule out the possibility of a recursive symbolic link. How could I find a symbolic link that is recursive at some level?
|
This related question, provides a way to identify a recursive symbolic link using the find command:
$ find -L .
.
./a
./a/b
./a/b/c
./a/b/c/d
find: File system loop detected; `./a/b/c/d/e' is part of the same file system loop as `./a/b'.
find: `./a/b/e': Too many levels of symbolic links
| How could I quickly find a recursive symbolic link? [duplicate] |
1,322,691,968,000 |
I am using the following Linux command to recursively count lines of text files in a folders structure:
find . -name '*.txt' | xargs -d '\n' wc -l
This outputs all found files and their number of lines:
86 ./folder1/folder11/folder111/file1.txt
67 ./folder1/folder11/folder112/file2.txt
7665 ./folder1/folder11/fol... |
If you have GNU wc, use
find . -name "*.txt" -print0 | wc -l --files0-from -
The manual section for this option explains why what you were doing doesn't work:
‘--files0-from=file’
Disallow processing files named on the command line, and instead process those named in file file; each name being terminated by a zero b... | Wrong result when recursively count lines with wc |
1,322,691,968,000 |
I am currently trying to set all files with extensions .html in the current directory and all its subdirectories to be readable and writeable, and executable by their owner and only readable (not writeable or executable) by groups and others. However, some of the files have spaces in their name, which I am unsure how ... |
Use the -exec predicate of find:
find . -name '* *.html' -type f -exec chmod -v u+rw,go+r {} +
(here, adding rw-r--r-- permissions only as it makes little sense to add execute permissions to an html file as those are generally not meant to be executed. Replace + with = to set those permissions exactly instead of addi... | How do I handle file names with spaces when changing permissions for certain files in the current directory and all its subdirectories? |
1,322,691,968,000 |
I am on a system which deletes files which haven't been modified in 30 days. I need some way to preserve important files by marking them as being recently modifed. What is the best way I can do this? Something like for d in *; do; cat $d > $d ; done
|
cd to that directory, then use this command to mark only the files :
find . -type f -exec touch {} \;
or this command to mark even the directories :
find . -exec touch {} \;
After the execution, the files (and folders if you choose the 2nd command) will be marked that they were just changed, and their content won't ... | recursively mark all files in a directory as modified without changing file content |
1,322,691,968,000 |
I have the following directory structure in AIX.
codeRepo/REPO1/AREA1/objects
codeRepo/REPO1/AREA2/SUBAREA1/objects
codeRepo/REPO1/AREA2/SUBAREA2/objects
From codeRepo I want to run chown myUser * on each objects directory in the tree. As you can see there are various objects directories sitting in different places.
|
If you want to chown the directories only (and not the subfiles), use find -exec, as like:
find -type d -name objects -exec chown myUser {} \;
Going through this:
-type d selects only directories
-name objects looks only for directories named exactly "objects"
-exec chown myUser {} \; executes chown myUser {} for eac... | Run a command over directories with name recursively |
1,322,691,968,000 |
I wanted to recursively run chmod go+w in a particular folder including hidden files, and I first tried
find . -name ".*" -o -name "*" -exec chmod go+w {} \;
but I found that it wasn't affecting hidden files. To check myself, I ran just
find . -name ".*" -o -name "*"
and the hidden files were listed. I also noticed... |
The solution is to bind the two name tests together with parens.
To illustrate this, let's consider a directory with three regular files:
$ ls -a
. .. .hidden1 .hidden2 not_hidden
Now, let's the original command:
$ find . -name ".*" -o -name "*" -exec echo Found {} \;
Found ./not_hidden
Only the non-hidden file ... | Different behaviors between find -exec and piping through xargs [duplicate] |
1,475,911,180,000 |
I want to run this bash command :
#!/bin/bash
rep="*"
for f in `ls -R`$rep; do
d='git log '$f'| wc -l'
c=$d
echo $c
done
how to excute a command git log myFile | wc -l from bash ?
ps : this command will return a number : git log myFile | wc -l
|
If you want to execute a command and get the output use the line below
d=`git log`
In your script you have to change two two things. I have the correct script below
#!/bin/bash
rep="*"
for f in `ls -R $rep`; do
d=`git log $f| wc -l`
c=$d
echo $c
done
Edit: The original correction is changing the quotes to backticks ... | Run a command on all the files in a directory tree and put the output into a variable |
1,475,911,180,000 |
How can I do a line count on all the PHP scripts within my webroot?
I am trying something like this below to no avail:
wc -l *.php
|
You need to use either a shell whose wildcard expansion includes subdirectories, or to stack another tool for directory transversal, such as find:
find -name "*.php" | xargs wc -l
If, OTOH, your goal is to sum it all, join the code first:
find -name "*.php" | xargs cat | wc -l
| line count on all the PHP scripts within my webroot with wc |
1,475,911,180,000 |
I have a large n-level directory structured as follows:
root
|
subdir1
|
sub_subdir1
|
....(n-2 levels).....
|
file1
|
subdir2
|
sub_subdir2
|
....(n-2 levels).....
... |
Using zsh:
cd /root
for subdir in subdir*
do
mv "$subdir"/**/*(.) "$subdir"
rm -r "$subdir"/*(/)
done
This:
changes to the "/root" directory (from your example)
loops over every subdirectory named subdir* (again from your example: matching subdir1 and subdir1)
moves the (expected one, but would match all) matchi... | Recursively flattening subdirectories in a root directory and maintaining level 1 sub-directory structure |
1,475,911,180,000 |
I have the following file structure:
Some directory
Some file.txt
Another file here.log
Yet another file.mp3
Another directory
With some other file.txt
File on root level.txt
Another file on root level.ext
What I want to do now is run a little script that takes another file as input containing some type of pat... |
TOP="`pwd -P`" \
find . -type d -exec sh -c '
for d
do
cd "$d" && \
find . ! -name . -prune -type f -exec sh -c '\''
while IFS=\; read -r pat repl
do
rename "s/$pat/$repl/g" "$@"
N=$#
for unmoved
do
... | Recursively rename files by using a list of patterns and replacements |
1,475,911,180,000 |
When I do chmod _+x -R /dir where "_" is any combination of (u,g,o,a), if after I do chmod g+X -R /dir, the files gain executable permissions as well.
Why does this happen? This behavior only happens if I use lower "x" first, then use upper "X".
First example:
[root@jesc5161 home]# chmod a-rwx -R finance/
[root@jesc... |
+X means to set the execute bit:
if the file is a directory or if the current (unmodified) file mode bits have at least one of the execute bits (S_IXUSR, S_IXGRP, or S_IXOTH) set. It shall be ignored if the file is not a directory and none of the execute bits are set in the current file mode bits.
Once you've run ch... | Why is chmod recursively changing file permissions as well? |
1,475,911,180,000 |
I need to list files in hierarchy of directory. For that I wrote script like
foreach file ( * )
ls ${file}/*/*/*/*/*.root > ${file}.txt
end
But for this I have to know that in the directory ${file} there are 4 directory. So, Is there any way by which I can generalize this script, such that I did not have to be id... |
Use the find command.
foreach file ( * )
find ${file} -name "*.root" > ${file}.txt
end
Consider using a shell other than csh, which has been obsolete for about 20 years. Use zsh or bash interactively, and don't use csh for scripts.
| List files in hierarchy of directory |
1,475,911,180,000 |
I have a large directory with tons of files and subdirectories in it. Is there a way to recursively search through all of these files and subdirectories and print out a list of all files containing an underscore (_) in their file name?
|
find . -name '*_*'
Thanks to Stéphane Chazelas as noted in the comments above!
| Recursively list files containing an underscore in the file name |
1,475,911,180,000 |
I am looking for a specific file in OS/X.
I can see the file by using:
ls -alR | grep "mkmf.*"
This shows my that the file exists.
How do I find what directory the file is located.
Many thanks
|
Use find which is better suited for your intended purpose:
find . -name "mkmf*"
It will list all appearances of your pattern including the relative path. For more information look at manual page of find with man find or go to http://www.gnu.org/software/findutils/manual/html_mono/find.html
| How do I find a files directory? |
1,475,911,180,000 |
How do I make the following function work correctly
# Install git on demand
function git()
{
if ! type git &> /dev/null; then sudo $APT install git; fi
git $*;
}
by making git $* call /usr/bin/git instead of the function git()?
|
Like this:
# Install git on demand
function git()
{
if ! type -f git &> /dev/null; then sudo $APT install git; fi
command git "$@";
}
The command built-in suppresses function lookup. I've also changed your $* to "$@" because that'll properly handle arguments that aren't one word (e.g., file names with spaces)... | Install-on-Demand Wrapper Function for Executables |
1,475,911,180,000 |
I'm using:
grep -n -H -o -R -e textword .
List all file recursively under directory '.' with string 'textword' and show the file, line and only portion matching.
I need to remove lines that matched the text using a Linux command.
|
find . -type f -exec sed -r -i "/textword/d" {} +
Remember that the search text is interpreted as a regexp by sed (with the -r option), so it might need escaping.
Use sed -i.backup to backup original files as <filename>.backup.
| Search and replace full line in recursive files |
1,475,911,180,000 |
One of my website on my webserver has suffered an attack : code injection.
Here is the malicious code :
<script type=\"text/javascript\" language=\"javascript\">
(function () {
var t = document.createElement('iframe');
t.src = 'http://ahtiagge.ru/count27.php';
t.style.position = 'absolute'; ... |
You can simply use grep for the same
grep -RP "http:\/\/ahtiagge.ru\/count27\.php" /var/www/vhosts/
or only check in *.php files only with the help of find
find /var/www/vhosts/ -name "*.php" -print | xargs grep -P "http:\/\/ahtiagge.ru\/count27\.php"
| find "an expression" on each file of a directory recursively |
1,475,911,180,000 |
Trying to do something like (in pseudo-unix):
scp -r <pwd> [email protected]:~/<dirname of toplevel>
In other words, I'm trying to copy the current directory I'm in locally (and the contents) over to remote while sticking the very last path segment from "pwd" commands output onto /home/user/<here> in the remote.
I'm ... |
$ scp -pr "$(pwd)" [email protected]:"$(basename $(pwd) )/"
| Use SCP from local machine to recursively copy current working directory to remote? |
1,475,911,180,000 |
I quite like mercurial .hgignore-style pattern globbing.
Globs are rooted at the current directory; a glob such as *.c will only match files in the current directory ending with .c.
The supported glob syntax extensions are ** to match any string across path separators and {a,b} to mean "a or b".
Is there a Linux she... |
All shells will support the standard glob *.c. KSH, Bash, and ZSH support brace expansion ({a,b}), but note that this not a file glob, so it will always expand. ZSH's extended globbing and Bash's globstar (bash v4 or higher), support ** for recursive globbing.
| I quite like mercurial .hgignore-style globbing. Is there a Linux shell that supports it? |
1,475,911,180,000 |
For a Perl script I'm working on, I'm looking for a fast and reliable way to find all subdirectories (transitively) of a given directory which are leaves, i.e. those not having any subdirectories of their own. For example, given this hierarchy:
foo/
foo/bar/
foo/bar/baz
foo/you_fool
my hypothetical function, when cal... |
You could do something like:
perl -MFile::Find -le '
find(sub {
if (-d _) {
undef $leaves{$File::Find::name};
delete $leaves{$File::Find::dir};
}
}, ".");
print for keys %leaves'
undef sets hash element for the current dir to an undef value, while delete deletes the... | Detect leaf directories in Perl |
1,475,911,180,000 |
Why
I have two folders that should contain the exact same files, however, when I look at the number of files, they are different. I would like to know which files/folders are present in one, not the other. My thinking is I will make a list of all the files and then use comm to find differences between the two folders.... |
You don't need any of that, just use diff -qr dir1 dir2. For example:
$ tree
.
├── dir1
│ ├── file1
│ ├── file3
│ ├── file4
│ ├── file6
│ ├── file7
│ ├── file8
│ └── subdir1
│ ├── dsaf
│ ├── sufile1
│ └── sufile3
└── dir2
├── file1
├── file2
├── file3
├── file4
├── f... | Recursively list path of files only |
1,475,911,180,000 |
I am doing some pre-processing on files. I have 2 text files which contains data in the following format.
Text File 1
"Name","Age","Class"
"Total Students:","247"
"John","14","8"
"Sara","13","8"
Text File 2
"Name","Age","Class"
"Total Students:","119"
"John","15","9"
"Sara","16","9"
What I am trying to do is I am re... |
You can't conveniently run sed on multiple files and get it to write to more than one file in one go (if the input and output need to be separate files). It's possible using non-standard extensions or by hard-coding the names of the output files in the sed expressions.
Your operations are so simple though that we may ... | Apply same sed command on multiple text files |
1,475,911,180,000 |
I have a folder with a whole lot of folders with a whole lot of Markdown files in each. What I'd like to do is recursively (if possible) prepend the file name as a heading in each file.
So, given foo.md:
The cat sat on the carpet, which is really just endless mat to a cat.
I'd like:
# Foo.md
The cat sat on the carpe... |
Try bash's globstar and ed plus some P.E., I suggest you try with some sample data/directory first.
shopt -s globstar
for f in md/**/*.md; do
header=${f##*/} header=${header^}
printf '%s\n' 0a "# $header" "" . w | ed -s "$f"
done
Or the one liner but remember to set globstar
for f in md/**/*.md; do header=${f##... | Append file name to beginning of [Markdown] file recursively |
1,475,911,180,000 |
I have a PHP application with is located on Linux with multiple directories (and sub-directories) and many PHP, JS, HTML, CSS, etc files. Many of the files have Windows EOL control characters and I am also concerned that some might not be UTF-8 encoded but maybe ISO-8859-1, Windows-1252, etc. My desire is to convert... |
There's quite a few questions here rolled into one.
Firstly when using find I would always use --exec instead of xargs. As a general rule it's better to do things in as few commands as possible. But also the first two methods write all the file names out to a text stream ready for xargs to re-interpret back into fi... | Recursively converting Windows files to Unix files |
1,475,911,180,000 |
mv -Z applies the default selinux context. Does it differ from all other invocations of mv, and work on all the files in a moved directory individually?
|
Yes.
$ mkdir a
$ touch a/b
$ ls -Z -d a a/b
unconfined_u:object_r:user_home_t:s0 a
unconfined_u:object_r:user_home_t:s0 a/b
$ strace -f mv -Z a ~/.local/share/Trash/files
...
open("/home/alan/.local/share/Trash/files/a/b", O_RDONLY|O_NOFOLLOW) = 3
...
fgetxattr(3, "security.selinux", "unconfined_u:object_r:user_home_t... | Does `mv --context` (for selinux, a.k.a. -Z) correctly apply labels recursively to directory contents? |
1,475,911,180,000 |
I saw some of the posts on this website about how to download files from a directory recursively. So, I executed the following line:
wget -r -nH --cut-dirs=3 -A '*.bz2' -np http://www.xfce.org/archive/xfce-4.6.2/src/
It only downloads the index page and then deletes it automatically.
Output:
--2016-07-01 16:56:02-- ... |
Seems like it is not trivial to get directory listing over http; I could get the bz2 files using bellow:
wget -k -l 0 "http://archive.xfce.org/xfce/4.6.2/src/" -O index.html ; cat index.html | grep -o 'http://archive.xfce.org/xfce/4.6.2/src/[^"]*.bz2' | uniq -c | xargs wget
| wget not downloading files recursively |
1,475,911,180,000 |
I have a root folder with a text file in it called pairs.txt.
Within that root folder are other folders with text files called pairs.txt in them.
Is there a simple way to remove them using rm?
I know that there I could use find . -name 'pairs.txt' -exec rm {} \; but I would like to know of other ways, perhaps using * ... |
With bash 4+:
shopt -s globstar dotglob
rm -- **/pairs.txt
The globstar option makes ** match any number of directory levels. The dotglob option makes it include directories whose name begins with . (dot files).
With ksh93, use set -o globstar instead of shopt -s globstar. To get the effect of dotglob, use FIGNORE=.
... | Removing specific files recursively using rm or something simple? |
1,475,911,180,000 |
I have downloaded about 3200 websites to the depth 2. So now I have one master folder (abc) that contains many folders, containing files for each website. So my folder abc contains 3200 folders and each folder contains other folders that contains files with text from the websites. I have also a script that can edit te... |
Here is the final version of your command
find /home/student/eny/abc -type f -exec ./lynx.sh {} \;
Points to note:
-type f finds files only
you should specify path to your script ./ (dot slash) means current dir, you may want to specify the full path
lynx.sh should have executable bit set file mode 0755 would be fine... | How to find files and act on them (find + exec) |
1,425,972,805,000 |
Suppose, we have the following files in a directory:
test.txt
test.txt~
/subdir
test1.txt
test1.txt~
When I run rm -r ./*.*~ inside top dir only test.txt~ is removed.
Why doesn't it perform the recursive removal despite the fact that I used recursive flag?
You can reproduce my case with the following script:
#cre... |
*.*~ does not expand to any directories, it will just match any file or directory in the current directory that has a . in it somewhere and ends in ~
If you would like to find all the files that end in ~ from the directory you're in I would use find like
find -type f -name '*~' -delete
| Recursive rm doesn't work for me |
1,425,972,805,000 |
If I make a .tar.gz via
tar czvf - ./myfiles/ | pigz -9 -p 16 > ./mybackup.tar.gz,
Can I safely unzip an already gzip'd file ./myfiles/an_old_backup.tar.gz within the ./myfiles directory via
gzip -d mybackup.tar.gz
tar -xvf mybackup.tar
cd myfiles
gzip -d an_old_backup.tar.gz
tar -xvf an_old_backup.tar
? And can one ... |
If your question can be rephrased as "is it OK to have compressed
archives within compressed archives?", then the answer is "yes".
This may not be the most convenient (as you note, you will have to run
tar several files to get everything unpacked), and applying
compression to data that has already been compressed may ... | Is it safe to do recursive compression with tar, gzip and pigz? |
1,425,972,805,000 |
How can I delete all 'nohup.out' files within a directory recursively from my terminal? I'm using CentOS.
|
There can't be multiple files named nohup.out in a single directory, so I assume you mean that you want to remove it recursively:
find . -name nohup.out -exec rm {} +
If you are using GNU find, you can use -delete:
find . -name nohup.out -delete
In bash4+, you can also use globstar:
shopt -s globstar dotglob
rm -- *... | Delete all 'nohup.out' within a directory recursively |
1,425,972,805,000 |
How can I do a fast text replace with recursive directories and filenames with spaces and single quotes? Preferably using standard UNIX tools, or alternatively a well-known package.
Using find is extremely slow for many files, because it spawns a new process for each file, so I'm looking for a way that has directory t... |
You'd only want to use the:
find . -name '*.txt' -exec cmd {} \;
form for those cmds that can only take one argument. That's not the case of grep. With grep:
find . -name '*.txt' -exec grep foo /dev/null {} +
(or use -H with GNU grep). More on that at Recursive grep vs find / -type f -exec grep {} \; Which is mo... | Fast string replace in recursive directories |
1,425,972,805,000 |
I'm having trouble with grep's manner of interpreting "recursive" searching compared to cp. Maybe it's just that I use them differently.
In this, grep seems to interpret "recursive" differently from cp. Is that correct? (my question)
Per my Question to navigate through this on SO...
With files containing the grep-matc... |
Your grep command,
grep -R "find me" *.php
... contains a filename globbing pattern, *.php. This will be expanded by the shell before the shell executes grep, so the actual command at execution may look something like
grep -R "find me" file1.php file2.php inc.php
... where inc.php happens to be a directory name.
Th... | Do grep and cp treat "recursive" the same? |
1,425,972,805,000 |
I have a bash script below that is intended to curl against a list of domains, from a file, and output the results.
#!/bin/bash
baseurl=https://csp.infoblox.com
domains=/home/user/Documents/domainlist
B1Dossier=/tide/api/data/threats/state/host?host=$domains
APIKey=<REDACTED>
AUTH="Authorization: Token $APIKey"
... |
You can read the domains from file to an array and loop for them.
baseurl="https://csp.infoblox.com"
B1Dossier="/tide/api/data/threats/state/host?host="
url="${baseurl}${B1Dossier}"
# read domains to an array
mapfile -t domains < /home/user/Documents/domainlist
# loop for domains
for d in "${domains[@]}"; do
cur... | Curl against list domains from a file not working |
1,425,972,805,000 |
I want to recursively crawl a directory and if the file has \x58\x46\x53\x00 as the first 4 bytes, I want to run strings on the file.
|
h_signature=$(echo 58465300 | tr 'a-f' 'A-F')
read -r x a b x <<<$(od --endian=big -N 4 -t x2 yourfile | tr 'a-f' 'A-F')
case "$a$b" in "$h_signature" ) strings yourfile ;; esac
Meth-2:
dd if=yourfile count=4 bs=1 2>/dev/null |
perl -lpe '$_ = uc unpack "H*"' | xargs test "$h_signature" = && strings yourfile
Meth-3:... | If file data starts with a certain hex sequence, run strings command on the file |
1,425,972,805,000 |
I have a filestructure with several subfolders where I'd like to search for all subfolder containing a certain string ("sub*") and then move all of the files in these found folders up one level from each of their respective location. And even potentially delete the then empty folder but I could do that with a second s... |
This should do it:
find /path/to/base/folder/ -type d -name 'sub*' -exec bash -c 'mv {}/* "$(dirname {})"' \;
NOTE: this will not move hidden files (whose name start with .)
| Move Files from Directory up one level |
1,425,972,805,000 |
A previous answer to a post mentions to run sha1 hashes on an images of a dd drive clone image.
Another answer to that post suggests mounting the dd image an then compare if the sha1 hashes of "important files" matches.
I want to use the second approach, but instead of manually selecting files I would like to use a bu... |
As I understand your question you want to find out whether N random files differ between two file system paths. Comparing the files should be faster than calculating checksums of both files. Here is how you can do it:
#!/bin/sh
list1=/tmp/list1
list2=/tmp/list2
shuflist=/tmp/shuflist
n=100000 # How many files to compa... | check two partitions by selecting random files and running sha1 hashes on two files of each partition |
1,425,972,805,000 |
I am replicating a server -> I want to delete all the files inside the folders and the subfolders, without deleting the directories themselves.
Example:
home\apps\Batches\hello.txt
home\apps\Batches\test\text.txt
Output:
home\apps\Batches\
home\apps\Batches\test\
Only the files should be deleted.
|
Using \ to separate directory name components is a windows thing.
I would do something like:
find . -type f -delete
Note: That command is highly dangerous, be sure you really want to do it, and make sure you're in the correct directory (as I tell find to start in . the correct directory is the root of the subtree that... | Delete all the files in a folder and its subfolders without deleting the folder structure |
1,425,972,805,000 |
I am trying to find all the files in a directory structure which contain a specific word, but it is not working correctly.
For example: when run from the path /Institute/IITDhanbad/, the command
grep -rn "Programmer" *
giving result as
BTECH/CompScience.txt:22: Sudip is a Programmer
However, when run from the p... |
CSP is a symlink (see l in lrwxrwxrwx). Apparently your grep does not follow symlinks with -r, except symlinks provided as arguments. When you do:
grep -rn "Programmer" *
in the parent directory of CSP (i.e. in /Institute/IITDhanbad/MTECH/, right?), CSP appears as an argument after the shell expands *. But if you do ... | "grep -rn" only yields one of two expected results |
1,425,972,805,000 |
I'm using Bash on Ubuntu on Windows to run linux commands on my windows system. It seems to work just like Ubuntu so for all intents and purposes I'm on ubuntu here I guess.
I'm trying to use strings to dump all the strings across a bunch of files in a directory/its subdirectories to a single file.
I'm using this thre... |
The find command works fine, you only need to exclude all.jp.txt from the list of files to be found or redirect the output to a different directory, i.e. not . or one of its sub-directories. Otherwise strings also runs on all.jp.txt and grows and grows.
find . -type f ! -path ./all.jp.txt -exec strings -e S {} \; > al... | Output file blows up when trying to find strings on bash on ubuntu on windows |
1,425,972,805,000 |
I wrote an executable that I want to execute on all the files contained in a directory.
This is what the program looks like:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <string.h>
#include <errno.h>
int main(int argc, char const *argv[]){
char argNormaux[4096] = ... |
It looks as if you want the file path to be the first argument to your program:
find testDir -type f -exec ./logic {} essai \;
This will search the directory testDir (as well as any subdirectory of testDir) for regular files, and for each found file, it will call your program with the pathname of the file as the firs... | How to execute recursively a program on every files contained in directory with find |
1,425,972,805,000 |
I want to run this:
grep -r zlib *.cpp
But this fails unless there is a .cpp file in the current directory (in which case only it is searched):
grep: *.cpp: No such file or directory
Now:
grep -r zlib *
Does search through the whole hierarchy but I want to limit the search to (eg) .cpp files.
Looked through man an... |
It has nothing do with grep, but with your current shell. The shell expands *.cpp to all the cpp file names in your current directory before running the grep command. Since it can't find any filenames, the glob *.cpp is left unexpanded by the shell. The grep command tries to see this is a file, but cribs that it is no... | Grepping across multiple directories |
1,425,972,805,000 |
The program "zip" has a -R feature which allows one to zip all files with a certain name in a directory tree: zip -r v/s zip -R
For example:
zip -R bigfile "bigfile"
Will zip all of the following:
./bigfile
./a/bigfile
./a/b/bigfile
./a/b/c/bigfile
.......
The -R feature doesn't seem to be in gzip or xz though.... |
Combining find, tar and the compression utilities:
With gzip:
find . -type f -name bigfile | tar cfz bigfile.tgz -T -
or with xz:
find . -type f -name bigfile | tar cfJ bigfile.txz -T -
find searches recursively for all files named bigfile under the current/working directory and the resulting pathnames are supplied ... | How may I emulate the -R feature of "zip", but in gzip and xz? |
1,425,972,805,000 |
I have lots of tar(.tar.bz2) files containing multiple file types in a recursive directory.
I want to extract just one type of file(let's say .txt files) from all the directories. How do i do that?
I have this command to extract all the files in each directory:
for file in *.tar.bz2; do tar -jxf "${file}"; done
I wa... |
Quoting the GNU tar manpage:
Thus, to extract files whose names end in `.c', you can use:
$ tar -xf foo.tar -v --wildcards '*.c'
So for you case, I'd go with:
for file in *.tar.bz2; do tar -jxf "${file}" --wildcards '*.txt'; done
| how to untar specific files recursively in linux? [duplicate] |
1,425,972,805,000 |
This question has parallels to question "touch all folders in a directory".
How to touch everything in a directory,
recursively
including hidden entries, like "directory up" .. and .
without de-referencing symbolic links touch -h and
use a reference file touch -r <file> as the the time stamp source
from within a she... |
If your touch command supports -h for no dereference:
find . -depth -exec touch -h -r "$reference_file" {} +
touch -c -h -r "$reference_file" ..
(note that -h implies -c (to avoid creating the file if it didn't exist with NetBSD/FreeBSD, but not with GNU or busybox touch (though with GNU touch it wouldn't create fil... | How to touch everything in a directory including hidden, like directory up `..`? |
1,425,972,805,000 |
I want to execute a script on startup. The script is recursive and calls itself.
When I added systemd unit for the same, it is executing but getting stopped in 1 min due to DefaultTimeoutStartSec. It assumes that my script has not yet started.
Following is the service file I have created
[Unit]
Description =... |
The issue is you have set the service type to forking. Systemd is waiting for you ExecStart to fork to background before proceeding. You need to change the type to simple. See the manual
| Start Recursive Script as systemd unit |
1,425,972,805,000 |
There are about 50 HTML/js files in the folder name site,
some of the files contain (below lines are combined from files)
{"rendered":"http:\/\/localhost:4542\/?page_id=854"}
http:\/\/localhost:4542\/wp-content\/uploads\/2022\/09\/
src=\"http:\/\/localhost:4542\/wp-content\/uploads\/202... |
You can try using the command find with -exec argument:
find /path/to/folder -type f -regex '.*\.\(js\|html\)' -exec sed -i 's#http:\\/\\/localhost:4542#https:example.com#g' {} +
On Mac Os you can use a similar syntax:
find /path/to/folder -E -type f -regex '.*\.(js|html)' -exec sed -i '.bak' 's#http:\\/\\/localhost:... | How to replace all matched strings in the files recursively? |
1,425,972,805,000 |
I have a folder that has many subfolders in it. I want to list all the files in all the subfolders but I want the files sorted by subfolder name. I'm not interested in files in the current directory.
Currently I am using this command which gets me the correct filename & path output I desire, with the exception that it... |
Pipe the data from find into sort. The default setting for sort is according to your locale, typically alphanumeric. If that's not giving you the sorting order you want, and you have GNU sort, try with the -V flag as in my example,
find -type f | sort -V
See man sort for the details
| List all files in subdirectories sort by subdirectory name |
1,425,972,805,000 |
I have a large file structure with multiple nested folders. I would like to tar+gzip up every folder with a specific name and remove the original folder.
In this example the script I hope to have would detect every folder ending in .abc and tar+gzip that folder up, removing the original folder and leaving .gz.
Main Fo... |
Run this in Main Folder:
find . -type d -name '*.abc' -exec sh -c '
for d; do tar -C "$d" -zcf "$d.tar.gz" . && rm -r "$d"; done
' findsh {} +
Although I tested it, I recommend you to test it too. Put an echo before rm, so if anything goes wrong you do not lose your data.
The inner loop executes tar and rm (only ... | How do I recursively tar+gzip folders with a specific name? |
1,425,972,805,000 |
I grasp the heading of this question odd, but I do wonder if in some situations there should be a need to take extra caution and somehow "enforce" non-recursivness when changing permissions with chmod nonrecursively (without the -R argument).
Say I have a directory ~/x. This dir has a few files, as well as a sub-dir ~... |
You can use find and restricting to get only files in the current directory
find ~/x -maxdepth 1 -type f -exec chmod +x {} +
| Make all files in a dir executable (non-recursively) while strictly-ensuring non-recursivness |
1,425,972,805,000 |
I need to delete recursive subfolders in a single line.
For one subfolder:
find folder -name "subfolder" -exec rm -r "{}" \;
or
find folder -name "subfolder" -type d -exec rm -r "{}" \;
But in the case of several subfolders in a single line? (subfolder1, subfolder2 or foo, bar, dummy…)
|
What I would do :
find folder -name "subfolder[0-9]*" -exec rm -r {} \;
using a glob
or
find folder \( -name 'foo' -o -name 'bar' -o name 'base' \) -exec rm -r {} \;
| Delete recursive subfolders with find |
1,425,972,805,000 |
I'm new to shell, and UNIX / GNU/Linux.
I'm trying to understand this syntax that is part of a recursion function:
[ $i -le 2 ] && echo $i || { f=$(( i - 1)); f=$(factorial $f); f=$(( f * i )); echo $f; }
(taken from here)
the full related function is:
factorial(){
local i=$1
local f
declare -i i
declare -i ... |
First,
local i=$1
assigns the value of $1 to i. $1 is the first argument of the function. Or outside of a function, it stands for the first positional parameter of a command or script.
The following are quite intuitive. -i means the variable is an integer.
local f
declare -i i
declare -i f
Now to the long one.... | explain recursion syntax |
1,425,972,805,000 |
I'm attempting to get a list of all files under specific folders, recursively, with the "find" command.
find / -path "/usr/sbin/*" -o -path "/usr/bin/*" -o -path "/usr/local/sbin/*"-o -path "/usr/local/bin/*" -o -path "/sbin/*" -o -path "/bin/*" -o -path "*/etc/*"
The previous command, doesn't list the contents of /... |
/sbin is a symlink to /usr/sbin and /bin is a symlink to /usr/bin.
ls -ld /bin /sbin will show you this.
| "find" doesn't list all files under specific directories |
1,425,972,805,000 |
I have wrote the following script:
#!/bin/bash
if [ $# -eq 0 ]
then
read current_dir
else
current_dir=$1
fi
function print_tree_representation ()
{
for file in `ls -A $1`
do
local times_p=$2
while [ $times_p -gt 0 ]
do
echo -n "----"
times_p=$(( $time... |
The problem is with this line:
if test -d $file
The $file you have extracted from ls -A doesn't contain the full path. You can fix it by replacing that line with
if test -d "$1/$file"
There's another bug, which is that it'll break all over the place if a filename has spaces in it. Put filenames in quotes.
| Bug in shell script for printing the tree-like structure of the curent diretcory |
1,425,972,805,000 |
I use grep -r all the time to find occurrences of a string within files in a given directory:
$ grep -r "string" app/assets/javascripts
> app/assets/javascripts/my_file.js: this line contains my string
But what if I want to recursively search through more than one subdirectory of my current directory? The only way I... |
You can specifiy multiple directories in grep:
grep -r "string" app/assets/javascripts spec/javascripts
Alternatively - sometimes more useful is list files to grep by find, and then grep them, for example
find app/assets/javascripts spec/javascripts -type f -print0 |
xargs -0 grep "string"
or
find app/assets/java... | How can I recursively grep through several directories at once? |
1,425,972,805,000 |
With this command
find . -name '*.zip' -exec unzip '{}' ';'
we find all zip files under . (directory), then unzip them into the current working directory,
However, the structure is gone.
/backUp/pic1/1-1.zip
/backUp/pic1/1-2.zip
/backUp/pic2/2-1.zip
/backUp/pic2/2-2.zip
/backUp/pic3/3-1.zip
/backUp/pic3/3-2.zip
/bac... |
You can use find with the -execdir argument instead of -exec.
-execdir will run the command in the directory it has found the files in, instead of your current working directory
Example:
find . -name '*.zip' -execdir unzip {} \;
While this is not exactly what you've asked for, you could add a few steps to get to the d... | For all zips, and unzip in working directory and maintains its structure |
1,425,972,805,000 |
Let's suppose I have the following timestamp like directory tree:
root
|__ parent1
| |__ 2021
| | |__ 01
| | | |__ 22
| | | | |__ 12H
| | | | | |__ file1
| | | | | |__ file2
| | | | |__ 13H
| | | |... |
Using find and a perl one-liner:
This use a tab to separate the timestamp and the filename, and NUL to separate each record - so will work with any filenames, including those containing newlines.
find .. -type f -printf '%T@\t%p\0' |
perl -MDate::Format -0ne '
($t,$f) = split /\t/,$_,2;
(undef,$p) = s... | Recursively traverse directories and retrieve last timestamp file |
1,425,972,805,000 |
Just for curiosity, is it possible to delete contents in a directory which is mounted inside of itself OR in a folder inside it?
For example, I was taking a backup of my Arch installation with Timeshift. I saw that Timeshift mounts / at /run/timeshift/backup/ temporarily. In this case, can I delete contents in my / mo... |
I just tried what you described on Debian inside my user directory. Made a directory test and another directory inside it test/mnt. Then added some test/content. And mounted test to test/mnt like this:
$ sudo mount --bind /home/user/test/ /home/user/test/mnt/
Now, if I delete it like this:
$ rm -r test/*
Or like thi... | Is it possible to delete contents of a directory mounted inside itself? |
1,425,972,805,000 |
I wrote a simple script to delete useless files from a directory using the find command.
Now I'm wondering if I can add lines to the script to flatten directories, but not completely flattened.
e.g.,
If I have a big directory named cleantarget:
cleantarget
Folder A
Folder A1
Folder A2
Folder B
Folder B1
Fold... |
These lines should work:
find "/path/to/cleantarget" -maxdepth 1 -mindepth 1 -type d | while read line; do
find "${line}" -mindepth 2 -type f -exec mv -t "${line}" -i '{}' +
rm -r "${line}"/*/
done
This will flatten Folder A and Folder B, asking if you want to overwrite duplicates, and remove the folders afte... | How do I write a shell script to flatten directories? |
1,425,972,805,000 |
I want to change the ACL and the default ACL for all directories and files in a base directory. In other answers (such as this one), the -R flag is used. However, I get
$ setfacl -R -m u::rwx my_dir/
setfacl: unknown option -- R
Try `setfacl --help' for more information.
# this is different from what's done on, e.g. ... |
To repeat the command for any file and directory contained in a directory you can use find and its -exec option
find my_dir -exec setfacl -m u::rwx {} \;
| setfacl -R doesn't work on Cygwin |
1,425,972,805,000 |
I have been trying to download specific pages in website.
The site uses common URL to go to next pages like below.
https://example.com/pages/?p=1
https://example.com/pages/?p=2
https://example.com/pages/?p=3 upto 450.
I just want to download those pages and not the hyperlinks that are linked with in the pages (mean no... |
Using a shell that understand arithmetic ranges in brace expansions (e.g. bash and ksh93 and zsh):
wget --load-cookies=cookies.txt "https://example.com/pages/?p="{1..450}
This would be expanded (before wget is called) to
wget --load-cookies=cookies.txt "https://example.com/pages/?p="1 "https://example.com/pages/?p="2... | wget only parent pages using {..} |
1,425,972,805,000 |
I'm running this command to find all the files named deploy.php in my whole project and make a copy of them and place them in the same directory as they were found, with name deploy_bkp.php
find . -type f -name "deploy.php" -exec cp {} deploy_bkp.php \;
But it's not working recursively, it's only working for file... |
According to https://askubuntu.com/questions/497122/find-and-exec-in-found-folder you should use -execdir
Your command should look like this:
find . -type f -name "deploy.php" -execdir cp {} deploy_bkp.php \;
| find and copy exec command not recursive [duplicate] |
1,302,715,144,000 |
I've not used dd all that much, but so far it's not failed me yet. Right now, I've had a dd going for over 12 hours - I'm writing an image back to the disk it came from - and I'm getting a little worried, as I was able to dd from the disk to the image in about 7 hours.
I'm running OSX 10.6.6 on a MacBook with a Core ... |
You can send dd a certain signal using the kill command to make it output its current status. The signal is INFO on BSD systems (including OSX) and USR1 on Linux. In your case:
kill -INFO $PID
You can find the process id ($PID above) with the ps command; or see pgrep and pkill alternatives on mac os x for more conven... | How do I know if dd is still working? |
1,302,715,144,000 |
I always use either rsync or scp in order to copy files from/to a remote machine. Recently, I discovered in the manual of scp (man scp) the flag -C
-C Compression enable. Passes the -C flag to
ssh(1) to enable compression.
Before I discovered this flag, I used to zip before and then scp.
Is it as effi... |
It's never really going to make any big difference, but zipping the file before copying it ought to be a little bit less efficient since using a container format such as zip that can encapsulate multiple files (like tar) is unnecessary and it is not possible to stream zip input and output (so you need a temporary file... | What does the `-C` flag exactly do in `scp`? |
1,302,715,144,000 |
I was wondering if there is a way to use Samba to send items to a client machine via the command line (I need to send the files from the Samba server). I know I could always use scp but first I was wondering if there is a way to do it with Samba. Thanks!
|
Use smbclient, a program that comes with Samba:
$ smbclient //server/share -c 'cd c:/remote/path ; put local-file'
There are many flags, such as -U to allow the remote user name to be different from the local one.
On systems that split Samba into multiple binary packages, you may have the Samba servers installed yet ... | Sending files over Samba with command line |
1,302,715,144,000 |
I want to allow incoming FTP traffic.
CentOS 5.4:
This is my /etc/sysconfig/iptables file.
# Generated by iptables-save v1.3.5 on Thu Oct 3 21:23:07 2013
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [133:14837]
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m state --state ESTABLISHE... |
Adding NEW fixed it, I believe.
Now, my iptables file look like this..
# Generated by iptables-save v1.3.5 on Thu Oct 3 22:25:54 2013
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [824:72492]
-A INPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp ... | Iptables to allow incoming FTP |
1,302,715,144,000 |
scp works well in all cases, but the Raspberry Pi is to weak to copy files efficiently in a secure environment (lan). The theoretically possible 6,75 MB/s via 54 Mbit wireless lan shrink down to about 1.1 MB/s.
Is there a way to copy files remotely without encryption?
It should be a cli command with no dependencies t... |
You might be looking for rcp, it performs remote execution via rsh so you will have to rely on that and have in mind that all communication are insecure.
| Copy files without encryption (ssh) in local network |
1,302,715,144,000 |
In a case I can use only UDP and ICMP protocols, how can I discover, in bytes, the path MTU for packet transfer from my computer to a destination IP?
|
I believe what you are looking for, is easiest gotten via traceroute --mtu <target>; maybe with a -6 switch thrown in for good measure depending on your interests.
Linux traceroute uses UDP as a default, if you believe your luck is better with ICMP try also -I.
| Discover MTU between me and destination IP |
1,302,715,144,000 |
I have a home file server that I use Ubuntu on.
Recently, one of my drives filled up so I got another and threw it in there.
I have a very large folder, the directory is about 1.7 T in size and contains a decent amount of files.
I used GCP to COPY the files from the old drive to the new one and it seems to have worked... |
I’d simply use the diff command:
diff -rq --no-dereference /path/to/old/drive/ /path/to/new/drive/
This reads and compares every file in the directory trees and reports any differences. The -r flag compares the directories recursively while the -q flag just prints a message to screen when files differ – as opposed to... | Verifying a large directory after copy from one hard drive to another |
1,302,715,144,000 |
Here is the situation:
I am uploading a large file from client A to a server using sftp.
I also need to download this file from the server to client B over ssh.
What I would like to do is start the transfer from the server to client B when the upload is still happening from client A.
What is the best method/tool to ... |
For a single file instead of using SFTP you could pipe the file over ssh using cat or pv at the sending side and using tee on the middle server to both send the data to a file there and send a copy over the another ssh link the other side of which just writes the data to a file. The exact voodoo required I'll leave as... | How to copy a file that is still being written over ssh? |
1,302,715,144,000 |
I have two servers. One of them has 15 million text files (about 40 GB). I am trying to transfer them to another server. I considered zipping them and transferring the archive, but I realized that this is not a good idea.
So I used the following command:
scp -r usrname@ip-address:/var/www/html/txt /var/www/html/txt
B... |
As you say, use rsync:
rsync -azP /var/www/html/txt/ username@ip-address:/var/www/html/txt
The options are:
-a : enables archive mode, which preserves symbolic links and works recursively
-z : compress the data transfer to minimise network usage
-P : to display a progress bar and enables you to resume partial transfe... | Transferring millions of files from one server to another |
1,302,715,144,000 |
I'd like to copy squid.conf from one server to another.
The servers don't talk to each other. I'd like to go through my workstation.
Both servers have the file, so it will be overwritten on the target.
The files have 600 permission and are owned by root.
root login via ssh is disabled (PermitRootLogin no).
I'd like t... |
It's easier to chain ssh with ssh than to chain ssh with sudo. So changing the ssh server configuration is ok, I suggest opening up ssh for root of each server, but only from localhost. You can do this with a Match clause in sshd_config:
PermitRootLogin no
Match Host localhost
PermitRootLogin yes
Then you can set... | Copying protected files between servers in one line? |
1,302,715,144,000 |
I need to upload a directory with a rather complicated tree (lots of subdirectories, etc.) by FTP. I am unable to compress this directory, since I do not have any access to the destination apart from FTP - e.g. no tar. Since this is over a very long distance (USA => Australia), latency is quite high.
Following the adv... |
lftp would do this with the command mirror -R -P 20 localpath - mirror syncs between locations, and -R uses the remote server as the destination , with P doing 20 parallel transfers at once.
As explained in man lftp:
mirror [OPTS] [source [target]]
Mirror specified source directory to local target directory. I... | How can I parallelise the upload of a directory by FTP? |
1,302,715,144,000 |
I have to make a configuration file available to guest OS running on top of KVM hyper-visor.
I have already read about folder sharing options between host and guest in KVM with 'qemu' and 9P virtio support. I would like to know about any simple procedure which can help in one time file transfer from host to guest.
Pl... |
Just hit upon two different ways:
Transfer files via network. For example you can run httpd on the host and use any web browser or wget/curl to download files. Probably most easy and handy.
Build ISO image on the host with files you want to transfer. Then attach it to the guest's CD drive.
genisoimage -o image.iso... | How to send/upload a file from Host OS to guest OS in KVM?(not folder sharing) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.