New answers tagged

0

I wrote two functions you can use together that do just that, you can limit the directory level by adding a -maxdepth $VAL parameter. # This scripts flattens the file directory # Run this script with a folder as parameter: # $ path/to/script path/to/folder #!/bin/bash rmEmptyDirs(){ local DIR="$1" for dir in "$DIR"/*/ do [ -d "${dir}" ]...


0

Here: while :; do cp /a/b/* /c/d/; sleep 60; done It copies the entire content of /a/b/ to /c/d/ every 60 seconds. It does have a drawback: If the files are big (more than a few GB?) then copying might take a while. If this is a problem, comment and I'll implement something optimised for big files (with tail -f) which will also redirect the content in ...


3

You need to specify the directory name for both source files: cp Assign/LinuxHist Assign/Linuxnotes.txt Resources (There are many more ways of writing this, but this is the simplest form of the command to understand). Now your title has "Copy" but the question says "move". The cp command will copy. If, instead you want to move the file then use the mv ...


2

With zsh: print -rl ./**/results.out(.e_'grep -q string $REPLY'_:h) this searches recursively for regular files (.) named results.out, runs grep -q ... on each of them and if that evaluates true it prints only the head of the path (the path without the last element). Another way with find and sh, using parameter expansion to extract the head: find . -...


4

If you have GNU find, you can print the path using the %h format specifier %h Leading directories of file's name (all but the last ele‐ ment). If the file name contains no slashes (since it is in the current directory) the %h specifier expands to ".". So for example you could do find . -name 'results.out' -...


1

for i in $(find . -type f -name "results.out); do grep -l "string1" $i ; exitcode=${?} if [ ${exitcode} -eq 0 ] # string1 is found in file $i then path=${i%/*} echo ${path} fi done


0

Assuming I understood correctly you want to do just that: find . -type f -name "results.out" -exec grep -l "string1" {} \; | xargs dirname First part gets matching filenames, then xargs passes those as an argument to dirname program which 'strips' filelame from path


1

You seem to have a directory whose name consists entirely of non-printable and whitespace characters. You can use a wildcard to match it: mv -i [^A-Za-z0-9_]* renamed This prompts you to move all files whose name don't begin with a letter, a digit or an underscore. There's probably only that one file. Alternatively, if your shell is set up to iterate ...


1

You have managed to create a directory with a space (or several) as its name. Rename it: mv " "* I_see_you In the case that it's not a simple space, you could try replacing any non graphical characters with X: $ # (having moved everything else away to a safe place...) $ for d in *; do test -d "$d" && echo mv "$d" "$( tr -c '[:graph:]' 'X' <&...


3

Assuming your shell supports parameter expansion: for f in *_*_*; do mv -i "$f" "${f#*_}"; done The parameter expansion pattern, ${f#*_}, removes the substring upto first _ from left Or using rename (prename): rename 's/^[^_]*_(.*)/$1/' *_*_* ^[^_]*_ matches the substring upto first _ from start and (.*) matches the rest, it is also put into the ...


3

You can use shell globbing for this: cp -rp *bat*/ /destination/ Here *bat*/ will expand to directories having bat in their names. Or using find, which will work even if there are so many files that you get an error because the command line is too long: find . -maxdepth 1 -type d -name '*bat*' -exec cp -rpt /destination {} +


0

If you're on RedHat, you probably have mlocate installed not Secure Locate. In that case there's no -e option and you have to use -r or --regexp to search using basic regular expressions.


2

If you have a file (named list containing, the following, and you with to create a directory for each line name in file, where the directory name is just the part after ./folder?/folder_. ./folder1/folder_01_PAP_515151 ./folder1/folder_04_PAP_654123 ./folder2/folder_055_PAP_685413 ./folder2/folder_100_PAP_132312 ./folder3/folder_32_PAP_3513131 ./folder3/...


1

A script which is well tested and supports a number of operations including finding duplicates, doing comparisons on both data and metadata, showing additions as well as changes and removals, you might like Fingerprint. Fingerprint right now doesn't produce a single checksum for a directory, but a transcript file which includes checksums for all files in ...


0

Try entr command-line tool which can run arbitrary commands when files change. Since 2.9 release, a directory watch option (-d) was added to react to events when a new file is added to a directory. Example to run the utility if a new file is added to the project: $ while true; do > echo src/* | entr -d your_command > done In directory watch mode ...


1

This is a direct translation of the find-config algorithm in generic shell commands (tested under bash, ksh, and zsh), where I use a return code of 0 to mean success and 1 to mean NULL/failure. function findconfig { # from: https://www.npmjs.com/package/find-config#algorithm # 1. If X/file.ext exists, return it. STOP # 2. If X/.dir/file.ext exists, ...


2

One way to do it: #! /bin/sh dir=$(pwd -P) while [ -n "$dir" -a ! -f "$dir/$1" ]; do dir=${dir%/*} done if [ -f "$dir/$1" ]; then printf '%s\n' "$dir/$1"; fi Replace pwd -P by pwd -L if you want to follow symlinks instead of checking physical directories.


3

A simple loop of checking the current directory and if it's not found then strip off the last component would work #!/bin/bash wantfile="$1" dir=$(realpath .) found="" while [ -z "$found" -a -n "$dir" ] do if [ -e "$dir/$wantfile" ] then found="$dir/$wantfile" fi dir=${dir%/*} done if [ -z "$found" ] then echo Can not find: $wantfile else ...


3

You also need to strip off the front of $dir with dir=GPS${dir#*GPS}. The resulting script: $ ls 121123_SN323_A_L002_GPS-100_R1.fastq.gz 121123_SN323_A_L002_GPS-100_R2.fastq.gz 121123_SN323_A_L002_GPS-104_R1.fastq.gz 121123_SN323_A_L002_GPS-104_R2.fastq.gz 130201_SN132_B_L007_GPS-100_R1.fastq.gz 130201_SN132_B_L007_GPS-100_R2.fastq.gz ...


1

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 Hope this help.


0

du naturally traverses the directory hierarchy and awk can perform the filtering so something like this may be sufficient: du -ak | awk 'BEGIN {sum=0} /\.jpg$/ {sum+=$1} END {print sum}' This works without GNU.


2

The trailing '/' on the source directory name is a subtlety of rsync. Pay attention to it. rsync A trailing slash on the source effectively means "copy the contents of this directory, not the directory itself". Without the trailing slash, it means "copy the directory". So rsync -a tmp/24/ host:/target will copy the contents of "/tmp/24/" into "host:/...


1

huh, so my problem can be solved like this: ssh host "mkdir -p /target" scp tmp/24/* host:/target


0

Do you have an entry for user1 in your /etc/passwd ? If you want to change the ownership of the directory you can type as root: chown apache:apache /var/www -R You can also add user1 to apache group and in this way your web server will fine. Maybe it's good idea to read the following topic: How can I create an SFTP user in CentOS?



Top 50 recent answers are included