Tag Info

New answers tagged

1

If you want to copy all the .txt files in a directory, use a wildcard pattern: cp direct/direct1/*.txt target This copies all the .txt files that are in the directory direct/direct1 to the directory target (which must already exist). You can pass multiple patterns to copy files from multiple directories: cp direct/direct1/*.txt direct/direct2/*.txt ...


2

You can use find to only select the `.txt files from under some directory: find direct/direct? -name "*.txt" this would print out all the files, so you can check you got what you wanted, and not too much is going to be selected. The *.txt has to be quoted, otherwise the shell will try expand this to .txt files in the current directory. As for the ...


0

The other answers here were fine but were insufficient for my needs. I needed a solution that I could use in my scripts on any machine. My solution was to write a shell script which I can invoke from the scripts where I need it. #!/bin/sh if [ $# -eq 0 ] || [ $# -gt 2 ]; then printf 'Usage: respath path [working-directory]\n' >&2 exit 1 fi ...


3

Assuming you are using bash (or zsh) and your mail agent is sendmail then: [[ -f "filename_$(date '+%m%d%Y')" ]] || echo "File is missing!" | sendmail myname@gmail.com Between [[ and ]] we test if file exists, and if not then print some message and send it to myname.


1

On Unix and Unix-like systems, . means the current directory. For example, ls . is the same as ls, it will list the contents of the current directory. So, when you run ./Desktop it finds a directory called Desktop that is under your current directory. You then get an error telling you that this is a directory and, therefore, cannot be executed as it is not a ...


0

The first shouldn't be an relative location. An relative location is to notate things shorter from within the location in which you're. An absolute location is from the root / which is the first directory location in Linux. When you aren't in /var you can use cd /var/www as /var/www an absolute location is. If you're in /var you can use cd ./www ...


1

Sample explanation: / (slash) means root, from the root of the filesystem. So, /home/yogesh/Desktop/Books starts from the root, then checks for home, under home check for yogesh and so on. This is called an absolute path. . (dot) means starting from current directory. So, if your current directory is /home/yogesh and you check for ./Desktop, it is there. ...


1

On unix (like on Windows), /foo is the location of a file or directory (absolute location, from the root directory: /). Whereas ./foo is a relative location (relative to the current directory)... You can omit the leading dot + slash and just write foo. By typing the command /Desktop or ./Desktop, you tell the shell to execute Desktop... Which can't be ...


0

You need to use --notest: convmv --notest -f cp850 -t utf-8 ./* From convmv manual: --notest Needed to actually rename the files. By default convmv will just print what it wants to do.


-1

easiest way ... echo file=machine_configuration.xml | cut -d '.' -f 1


2

Welcome to Linux! A trick that will get you started here (and will save you from getting carpal tunnel in the future) is "tab completion": $ ls /med then press Tab to see $ ls /media/ If you press Tab again, you might see a list of possible options to continue the path, $ ls /media/ MyBigExternalDrive/ My Example Hard Drive/ or (if there is only ...


2

About your errors: find: The relative path `~/program_files/internet/SSH_tunneling/' is included in the PATH environment variable, which is insecure in combination with the -execdir action of find. Please remove that entry from $PATH You have ~/program_files/internet/SSH_tunneling/ in your $PATH. That's a literal ~. That does not mean your home ...


2

If you want to stay with find and basename, this should work: find . -name "*.gif" ! -name "*t.gif" -execdir sh -c ' cp -- "$0" "$(basename "$0" .gif)t.gif"' {} \; This is performance- and resource-wise not the best option.


2

To make it even more compact and faster, you could use parallel: parallel mv {} {.}t.gif :::: <(find . -regex '.+[0-9].gif') The expression after :::: provides arguments for parallel. These arguments are then used using {}. The {.} represents the argument without the file-extension. So, in our case {} will be the file names of files without the t in ...


4

Yes, it's possible. One way to do it might look like this: cd /den/of/gifs && \ for f in ./*t.gif; do mv -- "${f%%t.gif}.gif" "$f" done The ${var%%pattern} thing is standard/POSIX sh syntax for removing the longest string that matches pattern from the end of $var.


0

Here is an example using one of your sample file names (but working on arbitrary file extensions): originalfilename=rh.cru_ts3.22.1901.2013.nc tmpfn=${originalfilename%.*} extfn=${originalfilename##*.} newfilename=${tmpfn}_cropped.${extfn} printf "%s\n" "${originalfilename}" "${newfilename}" Output: rh.cru_ts3.22.1901.2013.nc ...


0

You can use basename to remove the extension and then add the new extension: croppedname=$(basename $originalname .nc)_cropped.nc basename also removes the directory, so you can prepend your destination directory.


1

Shell script extensions are quite useful. For example I often write scripts that have multiple files in multiple languages (eg. bash, awk and lua) in the same directory. If I need to search for a string in only the bash files, the extension makes this very handy, to reduce false positives. Or if I want to do a line count of all my bash code for that project. ...


0

I'm surprised nobody mentioned the obvious zsh solution here yet: for file (**/*.csv(ND.)) { do-something-with $file } ((D) to also include hidden files, (N) to avoid the error if there's no match, (.) to restrict to regular files.) bash4.3 and above now supports it partially as well: shopt -s globstar nullglob dotglob for file in **/*.csv; do [ -f ...


1

You can use find ... -exec option: find . -name iaGambro_gambro.sql -exec bash -c ' select file do : Do something with "$file" break done ' bash {} +


1

Using xargs, it can be done in this way: find . -type f -print0 | xargs -0 file | grep -v 'image' But xargs is so yesterday. The cool kids use parallel today. Using parallel, it would be: find . -type f | parallel file | grep -v 'image' See. No use of -print0 and -0. parallel is really smart by itself. UPDATE For listing only the most recent 500 ...


1

For generic advice regarding processing of file names potentially containing spaces, see Why does my shell script choke on whitespace or other special characters? The difficulty with what you're trying to do is that there's no nice way to list the N most recent files with standard tools. The easiest way to do what you're doing here is to use zsh as your ...


1

Use "find" with "-print0" option & pipe the output to "xargs" with "-0" option. Even though I know (and use) this technique, I see that user @Jens has answered a similar question, where you can find more Details : http://stackoverflow.com/questions/16758525/use-xargs-with-filenames-containing-whitespaces


-1

You might try printf "%s\0" $(ls -t | head -500) | xargs -0 file | grep -v image This forces xargs to null-delimit the file name arguments.


1

I have two crude suggestions that might help. Neither feels particularly satisfying though, so perhaps something better will come up. First, use sed to add quotes to everything, so you'd only end up with trouble if there are quotes in the file name like ls -t | head -500 | sed -e 's/\(.*\)/"\1"/' | xargs file | grep -v 'image' The other is to use the ls ...


2

A crude way to do it: for f in /path/to/PDFs/*.pdf; do base=$( basename "$f" .pdf ) if [ ! -f /path/to/PNGs/"$base".png ]; then mv "$f" /path/to/garbage/ fi done


1

Using perl's rename (named prename in Ubuntu) prename 's/^([^_]*).*(_R.*)/$1$2/' *.fastq.gz


3

In bash or POSIX sh: for file in *.fastq.gz; do mv -- "$file" "${file%%_*}_${file##*_}" done


1

With zsh: autoload zmv # best in ~/.zshrc zmv '(*)_*(_R*)' '$1$2'


0

Some shell: this will be much slower than an awk program. cd /User/MyData for sample in Sample*.fasta; do sample_name=${sample%.fasta} while read name; read data; do name=${name#>} printf ">%s\n%s\n" "$sample_name" "$data" >> "$name.fasta" done < "$sample" done


2

To copy and truncate file names on the fly, you could do: cd /src && LC_ALL=C pax -rws'|\([^/]\{255\}\)[^/]*|\1|g' ./* /dst/ to truncate to 255 byte path components. Note that it may truncate a file name in the middle of a character if there are multi-byte characters in those file names. It will also update the targets of symlinks (though I'm ...


1

I have the following code for you; below it there's an explanation how it works. First go into the working directory (cd /User/MyData/) to run this program: awk ' FNR==1 { sample = FILENAME ; sub(/\.fasta/, "", sample } /^>/ { target = substr($0,2)".fasta" ; next } { print ">" sample > target ; print > target } ' Sample_*.fasta ...


0

Although it would be interesting to know what you've tried so far, here is an example how awk could be used for this job: awk ' FNR == 1 { sub(/\.fasta$/, "", FILENAME) } /^>/ && sub(/^>/, "") { newfile = $0 ".fasta" next } { print ">" FILENAME >> newfile print $0 >> ...



Top 50 recent answers are included