Hot answers tagged

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' -...


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 ...


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 ...


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 {} +


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.


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:/...


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/...


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 . -...


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.


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


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' <&...


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, ...


1

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


1

You can override the builtin compgen for simple directory completions (as for cd and ls). For example, put this function in your ~/.bashrc compgen(){ local IFS=$'\n' local x tmp x=$( builtin compgen "$@" ) && while read -r tmp; do [[ "$tmp" =~ ^/home/e && ! "$tmp" =~ ^/home/elsherbini$ ]] || echo "$tmp" ...


1

I'm using zsh which allows completion to be customized very thoroughly. This option prevents certain patterns from completion: autoload -Uz compinit; compinit # ignore some common patterns that we usually don't want as completions zstyle ':completion:*' ignored-patterns "/usr/share/iscan" "/usr/lib/iscan" \ "iscan" "/usr/lib/ispell" "ispell" "/usr/...


1

One way I'd suggest is to have an alias if we are just concerned about the home directory path, adding the below to your profile would help here, whatever unique alias you made should autocomplete with even a first character. alias myHome="cd /home/elsherbini" . .profile my\T pwd /home/elsherbini EDIT: The other way could be binding a key but even this ...


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 ...



Only top voted, non community-wiki answers of a minimum length are eligible