New answers tagged

0

You can simply do from the base directory you want to search cp $(grep -r -l "the pattern" *) target_directory grep -r means recusive -l means list the file path not the match in it. putting $() around the command will execute it and place the result in place in the outer command. cp will take a list of files, and as long as the last path is a directory ...


4

Do man xargs and look at the -I flag. find /path/to/directory -type f -exec grep -il "your pattern" {} \; | xargs -I % cp % /dest/dir/ To save typing put this command in shell script : #!/usr/bin/ksh # Script name: locate find /path/to/directory -type f -exec grep -il "$1" {} \; | xargs -I % cp % /dest/dir/ To run type locate searchstring where ...


0

Maybe because Windows shows file sizes in kbs, mbs, gbs, ... While linux shows them in kibs, mibs, gibs...? One kb is 1000 bytes, while one kib is 1024 bytes, and one mib 1024 kibs. Other possible reason is probably metadata. I am not sure however, because both of those reasons would suggest smaller file sizes in linux than in windows. Of course the ...


1

Your case statement could look like this S*) echo Starts with S if [[ -f x && -f x.csv ]] then echo File x and x.csv exist else echo input file missing fi ;;


-1

if you want to use an if clause, evaluate the count: if (( `ls *.txt 2> /dev/null|wc -l` ));then...


0

awk '{ORS=""; print $0}' textfile <div id="crmpicco"> <div class="ayrshireminis">... content in here ... </div></div> Additionally, as was already pointed out, the tr utility is awesome for this. To remove newlines and tabs/spaces all at once using the tr utility, do: # cat textfile |tr -d '\n\r" "' ...


0

The easiest tool here would be zsh (like bash, only better), with its fancy globbing and the zmv function. Run this from the toplevel directory where you want to rename files. autoload -U zmv zmv '(**/)[Ss][Ff][0-9]##-[0-9]##[- ]#(*)' '$1$2' Explanation: **/ is any string of leading directories. Then there's a pattern of sf (case-insensitive) followed by ...


0

I just came across the original answer here by @Mehmet while searching for something unrelated and I see that although it works, it is horribly inefficient, requiring each file to be read again for each unique word in all of the files! The second answer by @Jeff is rather convoluted despite the explanation and worst of all it suffers from the cat file | ...


1

One way with find and install: find /var/public/voicelogging/quality_monitoring -name \*.WAV -exec sh -c ' bn=${0##*/}; x=${bn%%-*}; dt=${x##*_}; y=${bn%_*}; id=${y##*_} install -D "$0" "/home/username/logging/${id}/${dt}/${bn}"' {} \; this uses parameter expansion to extract the date: ${dt} and the user id: ${id} from the filename and then uses install ...


0

Here is the code (replace directory by the main directory, or where you have the subdirs): cd directory find -type f | while IFS= read - file; do name=$(echo "$file" | rev | cut -d'/' -f1 | rev | cut -d'-' -f3- | cut -d' ' -f2-) path=$(dirname "$file") newname="${path}/${name}" echo mv "$file" "$newname" done Where directory is the ...


14

1) Borrowing from don_crissti's answer using tee, without dd or bashisms: sudo tee /dev/disk2 /dev/disk3 > /dev/disk4 < masi.img 2) Using pee from Debian's moreutils package: sudo dd if=masi.img | \ pee "dd of=/dev/disk2" "dd of=/dev/disk3" "dd of=/dev/disk4" With either method the number of output disks can be extended indefinitely.


11

You could try dcfldd It's an enhanced version of gnu dd and it can output to multiple files or disks at the same time: dcfldd if=masi.img of=/dev/disk2 of=/dev/disk3 of=/dev/disk4


3

Something like this: vimdiff <(find /home/masi -printf "%P %u:%g %m\n" | sort) <(find /home/masi_backup -printf "%P %u:%g %m\n" | sort) (this gives names without the leading /home/masi or /home/masi_backup, owning user and group, and permissions — the latter weren't mentioned in the question but seem useful, drop %m if you don't want them).


0

I hope it won't be a crime, to answer my own question. I've found partial solution at AskUbuntu - works for me, at least in the first case. touch -d "$(date -R -r filename) - 2 hours" filename And for modification of all files in subfolder, type: find DIRECTORY -print | while read filename; do # do whatever you want with the file touch -d "$(date ...


0

Finally I can do this with cp and its --backup flag. cp --backup=numbered */*.txt new_directory/


2

No, you can't. At least not directly. tar doesn't do any compression. It merely reads part of the (virtual) file system, and generates one cohesive stream from it. This stream is then often passed to a compression tool/library, for instance gzip/libz. The compression part does not see or even know about individual files. It just compresses the stream ...


0

Try this instead: find . -type d -exec sh -c "cd {} ; ls | grep -v '^filesNames.txt$' > filesNames.txt" \; This finds all directories beneath the current directory and, for each one, it cds into the directory and writes the directory listing to a filesNames.txt (excluding that file from the list). If you want the filesNames.txt files to be in the ...


2

If files are exactly the same, then their md5sums will be exactly the same, so you can use: find A/ B/ -type f -exec md5sum {} + | sort | uniq -w32 -D An md5sum is always exactly 128 bits (or 16 bytes or 32 hex digits) long, and the md5sum program output uses hex digits. So we use the -w32 option on the uniq command to compare only the first 32 ...


1

Per your comments, you only have to descend one level deep. In that case you could use a glob to iterate over directories names and for each dir save file paths in an array, then print the last element of each path to fileNames.txt: #!/bin/sh for dir in "$PWD"/*/; do arr=( "$dir"* ) printf %s\\n "${arr[@]##*/}" > "$dir"filesNames.txt done To exclude ...


1

You need to cd out of each subdirectory before trying the next one. Inserting... cd .. at the end of the loop would fix this for the subdirectories, but breaks when you do it in the top directory first. A cd .. from there will take you up another level, so the other directories aren't visible any more. You can address all of this by saving the top ...


0

#!/bin/bash for X in $PWD/* ; do if [ -d "$X" ] ; then # first depth directories cd "$X" ; files="$(ls)" ; printf "$files\n" >> filesNames.txt ; else # regular files: printf "$X\n" >> fileNames.txt ; fi ; done ; It may be that what you are trying to achieve ...


4

This is referring to the fact that Linux (like all Unix-style systems) exposes most of the resources it manages through objects which look like files: /dev-style devices, /proc and /sys entries... In the context of your quote, this property is mentioned because it allows access permissions to be reasoned about in a consistent fashion.


1

I think this will get you close. It will list out the cmp output for all files named results in A compared to all files named results in B. find ./A -name results | xargs -I REPLACESTR find ./B -name results -exec cmp REPLACESTR {} \;


1

startheader=$(head -1 sourcefile) endheader=$(tail -1 sourcefile) # above lines assume your sourcefile has two lines in it and # each line contains the starting header and ending header startlinenumber=$(grep -n "${startheader}" datafile|cut -d: -f1) endlinenumber=$(grep -n "${endheader}" datafile|cut -d: -f1) sed -n -e ...


1

If you want to monitor on a two second interval you can surround your check with: while true do <your steps> sleep 2 done While this will sequentially test for files and will wait 2 seconds for each file found I suggest to transform your check into a function: function _check_file() { SUM1=$(md5sum "$@") sleep 2 SUM2=$(md5sum ...


6

As others have explained, using inotify is the better solution. I'll just explain why your script fails. First of all, no matter what language you are programming in, whenever you try to debug something, the first rule is "print all the variables": $ ls file1 file2 file3 $ echo $PWD /home/terdon/foo $ for FILE in "${PWD}/*"; do echo "$FILE"; done ...


2

You can use inotify-tools definitely from command line, e.g. like this : inotifywait -r -m /dir/to/monitor/ From man inotifywait -m, --monitor Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs. And here is a script that monitors continously, copied from the man ...


2

You can use the inotify-tools package to monitor all changes in a folder in real time. For example, it contains the inotifywait tool, which you could use like : > inotifywait /tmp Setting up watches. Watches established. /tmp/ MODIFY test You can use flags to filter certain events only or certain files. The inotifywatch tool collects filesystem ...


2

The seeming challenges of the question / request is perhaps the recursion aspect. Assuming that cmp is an adequate utility and that both folder / directories 1 & 2 to be compared are of the same structure (ie same files & folders) and reside within the same root path - you can try something similar to: #!/bin/bash ROOT=$PWD ; # #// change to ...


0

Technically, almost anything is possible with ptrace() / gdb. Search results. Using ptrace() is arch-dependent, awkward, and AFAICT no-one's implemented it for you. The popular application is to inject an fd into the process, but that's about the opposite of what you want. I thought about what would be required and it looks very painful. You could use ...


1

With xargs you could run: <infile xargs -I {} mv gre_6_c1_{}.h3 /path/to/another/dir Or, with gnu parallel: cat infile | parallel mv gre_6_c1_{}.h3 /path/to/another/dir


0

With bash 4, you could load an array with file names derived from the numbers in the "driver" file and then use the -t option of GNU mv to move them into a directory at one shot. Note #read contents of driver_file into an array arr using mapfile mapfile -t arr <driver_file #define prefix and suffix variables prefix=gre_6_c1_d suffix=.h3 #prefix each ...


0

Using sed e flag : sed 's|^\(.*\)$|mv gre_6_c1_\1.h3 newdir/|e' list.txt Above will move the files to newdir/ change it accordingly. I assume on list.txt the number are as below separated by new line: d34567 d23168 Run this to confirm: sed 's|^\(.*\)$|mv gre_6_c1_\1.h3 newdir/|' list.txt


-1

Open the terminal and type the following Command : sudo chown -R $user:$group ./manifest.json


0

If you have ksh93 (for the arrays) and perl (for the timestamp/stat), then this will work: files=(*.xls) # exit early if there are no matching files [ "$files" = "*.xls" ] && exit 0 for index in ${!files[@]} do t[$index]=$(perl -e '$x=(stat(shift))[9]; print "$x"' "${files[index]}") done for i in ${!t[@]} do printf "%d %d\n" ${t[i]} $i done | ...


1

If you don't want to make any assumption on what character filenames may contain, you could do: ls -dt ./*.xls | awk -v q="'" -v n=10 ' function process() { if (NR > 1) { gsub(q, q "\\" q q, file) print "mv " q file q, q file "-bkp" q if (!--n) exit } } /\// { process() file = $0 next } {file = file "\n" ...


2

You should use -10 and not -1 as argument to head, and you also need quotes around -bkp, so ls -lt *.xls | head -10 | awk '{print "mv " $9 " "$9"-bkp"}' | sh should work. And you would probably have realised if you had tried removing | sh, so the command just ends with awk printing the commands.


-2

ls has a "sort"-parameter that can take a value of "time". #!/bin/bash IFS=$'\n' for file in $(ls *.xls --sort=time|head -n 10); do mv $file $file-bkp done unset IFS The IFS-shenanigans are because the for-loop becomes ill-mannered if you have spaces in your filenames. I believe there are more orthodox solutions to that issue, but my solution works. ...


0

You can replace every line of a file once it was processed correctly with this snippet (do not remove if processing fails, so you can reprocess them if you need it): while read; do if some_command_worked_correctly; then sed -i '1,1 d' file-to-read fi done < file-to-read


3

You don't need the all.txt file, sed, or a for loop. find and awk can do it all: find /path/to/folder -name data1.txt -exec awk -F': ' '/^Name/ {print $2}' + Using a field separator of : (colon followed by a space character) is the key to getting the output right (no leading space, supports one or more words in the second field) If you want the filename ...


0

I did the following and the code worked perfectly: #!/bin/bash PAT=PATH TO THE PARENT FOLDER for i in $(cat $PAT/all.txt); do echo -n "data1" sed -n '/Name:/p' $PAT/$i/data1.txt | awk '{ print $2}'


4

Here is a version that is safe for names with whitespace: find /var/log/folder -type f -printf '%T@ %p\0' | sort -rz | sed -Ezn '1s/[^ ]* //p' | xargs --null grep string How it works: find /var/log/folder -type f -printf '%T@ %p\0' This looks for files and prints their modification time (seconds) followed by a space and their name followed by a nul ...


3

ls -1rt /path/to/files/ | tail -n1 will find the newest file in a directory (in terms of modification time); pass that as an argument to grep: grep 'string to find' "$(ls -1rt /path/to/files/ | tail -n1)"


1

When using identify -format you must explicitly add a newline if you want one. Without it all your widths are on one line, and if the first one doesnt match your awk condition you will see nothing. ...-exec identify -format '%w %h %i\n' '{}' \; ...


1

It means that you have not done your homework properly. To see what you are doing wrong, look closely at your cp command, the requested action, and the manual page of cp.


1

Yes, you can treat the device file as a raw device file, and read/write data from/to it using the same APIs used to access normal files. In most cases, you can use dd, or simply cat data to/from the device file. Keep in mind that there are several practical differences between raw devices and real files on a filesystem: Devices have a fixed size. Writing ...


0

If I understand correctly, you have a file containing a list of URLs (one per line), and you want to pass those URLs to CURL. There are two main ways to do that: with xargs, or with command substitution. With xargs: xargs <urls.txt curl … With command substitution: curl … $(cat urls.txt) Both methods mangle some special characters, but given what ...


0

Check out this Answer Here is the link to the tool


1

From the test directory, do: mv -t player *.txt Assuming all text files end in .txt. This will mv all .txt files from current directory (test/) to player/ subdirectory.


0

With zsh: ( arr=(./*(.N^e_'grep -q PATTERN $REPLY'_)); print ${#arr}; ) This saves the names of the files that don't contain PATTERN into an array and returns the number of elements in the array. It uses glob qualifiers: . selects only regular files (add D to include hidden files), N turns on null glob and the negated (^) estring: ^e_'grep -q PATTERN ...



Top 50 recent answers are included