I'm having trouble with this piece of code:  22 for filename in "$( ls "$1" | grep ".*\.flac$" )"; do 23                 file_path="$1${filename}" 24                 ffmpeg -i "${file_path}" -f ffmetadata $HOME/metadata   Instead of a metadata file on each iteration, I'm getting this error message:     Downloads/Ariel Pink's Haunted Graffiti - Worn Copy (2005)/01   Trepanated Earth.flac ... 17 Jagged Carnival Tours.flac: File name too long   So it appears that inside the loop the $filename variable is equal the names of all FLAC files lumped together.  Of course, omitting quote marks on line 22 results in whitespace problems.  How do I make this work? I'm new to bash and very confused.
1847
Or, an introductory guide to robust filename handling and other string passing in shell scripts.  I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).  I encountered a problem such as the following:   I have a file name containing a space hello world, and it was treated as two separate files hello and world. I have an input line with two consecutive spaces and they shrank to one in the input. Leading and trailing whitespace disappears from input lines. Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is is actually the name of files. There is an apostrophe ' (or a double quote ") in the input and things got weird after that point. There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators).   What is going on and how do I fix it?
67648
I have a collection of files and directories with strange names, they contain spaces and the special characters \, *, ), ( and ?. All of them cause problems when I try to use them in the command line or scripts, e.g.:    $ cd space dir bash: cd: space: No such file or directory  $ cat space file cat: space: No such file or directory cat: file: No such file or directory  $ cat ( bash: syntax error near unexpected token `newline'  $ echo content &gt;\ &gt; ^C  $ ls ? (  )  *  ?  \   How do I enter file or directory names that contain special characters in the terminal?
27098
I once read that one way to obtain a unique filename in a shell for temp files was to use a double dollar sign ($$).   This does produce a number that varies from time to time... but if you call it repeatedly, it returns the same number.  (The solution is to just use the time.)  I am curious to know what $$ actually is, and why it would be suggested as a way to generate unique filenames.
111951
The idea would be to use it as... a pipe in a command. For instance: say there's some kind of long path which has to be retyped again and again, followed by a pipe and a second program, i.e.  "directory1/directory2/direcotry3/file.dat | less -I "   I'd like that part to be stored in a variable, so it could be used like this:  r="directory1/directory2/direcotry3 \| less -I -p " $ cat path1/path2/$r &lt;searchterm&gt;   Instead, I get  cat: invalid option -- I Try `cat --help' for more information.   ... meaning the pipe clearly didn't work.
100532
The runtime arguments are as follows: $1 is the path to the file containing the list of files $2 is the path to the directory containing the files What I want to do is check that each file listed in $1 exists in the $2 directory  I'm thinking something like:  for f in 'cat $1' do if (FILEEXISTSIN$2DIRECTORY) then echo '$f exists in $2' else echo '$f is missing in $2' sleep 5 exit fi done   As you can see, I want it so that if any of the files listed in $1 don't exist in the directory $2, the script states this then closes. The only part I can't get my head around is the (FILEEXISTSIN$2DIRECTORY) part. I know that you can do [ -e $f ] but I don't know how you can make sure its checking that it exists in the $2 directory.
58265
I have a bash script looping through the results of a find and performing an ffmpeg encoding of some FLV files. Whilst the script is running the ffmpeg output seems to be interupted and is outputting some strange looking errors like the one below. I've no idea what is going on here. Can anyone point me in the right direction?  It's as though the loop is still running when it shouldn't be and interupting the ffmpeg process.  The specific error is:  frame=   68 fps= 67 q=28.0 00000000000000000000000000001000size=      22kB time=00:00:00.50 bitrate= 363.2kbits/s dup=1 drop=0     Enter command: &lt;target&gt; &lt;time&gt; &lt;command&gt;[ &lt;argument&gt;] Parse error, at least 3 arguments were expected, only 1 given in string 'om/pt_br/nx/R3T4N2_HD3D_demoCheckedOut.flv'   Some more details from the ffmpeg output:  [buffer @ 0xa30e1e0] w:800 h:600 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:flags=2 [libx264 @ 0xa333240] using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64 [libx264 @ 0xa333240] profile High, level 3.1 [libx264 @ 0xa333240] 264 - core 122 r2184 5c85e0a - H.264/MPEG-4 AVC codec - Copyleft 2003-2012 - http://www.videolan.org/x264.html - options: cabac=1 ref=5 deblock=1:0:0 analyse=0x3:0x113 me=umh subme=8 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=2 b_bias=0 direct=3 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=50 rc=cbr mbtree=1 bitrate=500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=500 vbv_bufsize=1000 nal_hrd=none ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to './mp4s/pt_br/teamcenter/tc8_interactive/videos/8_SRM_EN.mp4':   Metadata:     audiodelay      : 0     canSeekToEnd    : true     encoder         : Lavf54.3.100     Stream #0:0: Video: h264 (![0][0][0] / 0x0021), yuv420p, 800x600, q=-1--1, 500 kb/s, 30k tbn, 29.97 tbc     Stream #0:1: Audio: aac (@[0][0][0] / 0x0040), 44100 Hz, mono, s16, 128 kb/s Stream mapping:   Stream #0:1 -&gt; #0:0 (vp6f -&gt; libx264)   Stream #0:0 -&gt; #0:1 (mp3 -&gt; libfaac) Press [q] to stop, [?] for help error parsing debug value0 00000000000000000000000000000000size=      13kB time=00:00:00.-3 bitrate=-3165.5kbits/s dup=1 drop=0     debug=0 frame=   68 fps= 67 q=28.0 00000000000000000000000000001000size=      22kB time=00:00:00.50 bitrate= 363.2kbits/s dup=1 drop=0     Enter command: &lt;target&gt; &lt;time&gt; &lt;command&gt;[ &lt;argument&gt;] Parse error, at least 3 arguments were expected, only 1 given in string 'om/pt_br/nx/R3T4N2_HD3D_demoCheckedOut.flv'   The script is as follows  #!/bin/bash LOGFILE=encodemp4ize.log echo '' &gt; $LOGFILE STARTTIME=date echo "Started at `$STARTTIME`" &gt;&gt; $LOGFILE rsync -avz flvs/ mp4s/ --exclude '*.flv' #find flvs/ -name "*.flv" &gt; flv-files # The loop find flvs/ -name "*.flv" | while read f do FILENAME=`echo $f | sed 's#flvs/##'` MP4FILENAME=`echo $FILENAME | sed 's#.flv#.mp4#'` ffmpeg -i "$f" -vcodec libx264 -vprofile high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -threads 0 -acodec libfaac -ab 128k "./mp4s/$MP4FILENAME" echo "$f MP4 done" &gt;&gt; $LOGFILE done
114806
I want to rename multiple files in the same directory using Bash scripting. Names of the files are as follows:  file2602201409853.p file0901201437404.p   file0901201438761.p   file1003201410069.p   file2602201410180.p   I want to rename to the following format:  file2503201409853.p file2503201437404.p   file2503201438761.p   file2503201410069.p   file2503201410180.p   I was reading about the rename command, and try to do it this way, but it does nothing, I think I have questions about the syntax. Then I read that you can make a loop using the  command as follows:  for file in cmpsms*2014*.p; do     mv "$file" "${file/cmpsms*2014*.p/cmpsms25032014*.p}" done   But I can not rename the files. What am I doing wrong?
79163
Dropbox tells me many of my files are not syncing because their name exceeds the max character length       Max character length      Windows only allows file and folder names of 260 characters or less.   Note that Windows counts the file path as part of the name, so a file   like C:\Users\Panda\My Documents\Dropbox\Creative Nonfiction\My   Autobiography\Favorite Things\Favorite Foods\Bamboo\Family   Recipes\Fresh Leaves.doc would be 142 characters, not 16. If the   entire file path and name exceed 260 characters, shorten the name or   move the file or folder to a higher-level folder within your Dropbox.   How do I find all the files under a directory whose file and folder names are greater than 250 characters?  (Note: the "Check bad files" tool on that page doesn't find them either.)  Update: there are two suggestions so far, one using powershell that ironically, died when a filename grew too large, the other using cygwin's find and xargs, which may work, but is still running (several minutes later.)  I did solve my immediate problem.  In an emacs shell window, I ran a very simple find  $ find . -type f -print &gt;&gt; ../files.log  Then I opened that in emacs and typed m-x list-matching-lines ^ ESC 230 . RETURN  Which displays lines that match a regular expression, and the regular expression here is that the line contain at least 230 characters, where 230 was just a rough guess for lines that might be problematical. And that showed me two files that were too long.  For about 20,000 files that regular expression filter turned out to be almost instantaneous.
123475
If I have a number of directories, named, for example 10001 through 10025 is there any reason to use ls 1*/foo vs. ls 100??/foo?  I have a lot more than 25 of them, so I mostly curious if there's any differences in speed.  I know the difference in use between the two, that the asterisk will match longer file names, like 10001.backup. But let's say I don't have any files that don't follow my conventions. Is there any behind-the-scenes differences?
93123
I am trying to batch-rename a bunch of files in my shell, and even though there is plenty of material about it on the internet, I cannot seem to find a solution for my specific case.  I have a bunch of files that have (what appears to be) a "timestamp-id":  abc_128390.png abc_138493.png abc_159084.png ...   that I'd like to exchange for a counter:  abc_001.png abc_002.png abc_003.png ...   My (plenty) naïve approach would be something like:  mv abc_*.png abc_{001..123}.png  Also, I could not figure out a way to make it work with a for-loop.  FWIW, unfortunately rename is not available on this particular system.  Any advice would be greatly appreciated!
23751
I want to create a simple bash-script that checks whether a directory contains all the files whose names contain numbers from 1 to N.  # Creating some files for testing $ cd /tmp/ $ mkdir test $ touch test/a01x.dat $ touch test/b02y.dat  # Display dir contents $ ls test/*{01,02}* test/a01x.dat  test/b02y.dat   But using seq command to generate numbers results in the following:  $ ls test/*{$(seq -s , -f "%02g" 1 2)}* ls: cannot access 'test/*{01,02}*': No such file or directory   I understand that running the command by surrounding the path with single quotation marks must lead to the error because the wildcards don't expand  $ ls 'test/*{01,02}*'   But I didn't use them. What is the problem?
8450
In Bash, suppose I visit a directory, and then another directory. I would like to copy a file from the first directory to the second directory, but without specifying the long pathnames of them. Is it possible?   My temporary solution is to use /tmp as a temporary place to store a copy of the file. cp myfile /tmp when I am in the first directory, and then cp /tmp/myfile . when I am in the second directory. But I may check if the file will overwrite anything in /tmp.  Is there something similar to a clipboard for copying and pasting a file?
90781
I want to replace cat:  var=$(cat "filename" 2&gt;/dev/null)   by bashism syntax:  var=$(&lt;"filename")   The problem is that I don't know how to make the bashism silent to avoid such warnings:  bash: filename: No such file or directory   I've tried this:  var=$(2&gt;/dev/null &lt;"filename")   but it does not read existing files into var anymore.
116673
For example, I have files named   A_1.txt A_t_1.txt A_ts_1.txt A_tsa_1.txt   in a directory ~/admin/packages/.  I am building a for loop to by globbing to A_1.txt   for f in ~/admin/packages/*_1.txt   It's obvious that this can potentially glob to any one of these files.  Specifically I would like to use an "exception" command (if it exists) to say glob to any file as long as it does not have a t, s, a before _1.txt.  EDIT: I forgot to mention that I have the path stored as a variable.  PROJECT="~/admin/packages/*_1.txt"  for f in ${PROJECT}...
87437
I'm trying to beat my head against the syntax of this. I'm getting problems with whitespaces and I can't see if there's a better way to do this.  vardir=/home/user cd $vardir for i in $(ls "$vardir") ; do timestamp="$(stat -c %Y $i)" echo "$i" echo "$timestamp"  done   Result is similar no matter where I quote and I'm not sure what I'm doing wrong:  stat: cannot stat ‘Temporary’: No such file or directory Temporary  stat: cannot stat ‘Items’: No such file or directory Items   My goal is I want to grab the timestamps off of backup directories and touch the current ones to restore the original modified dates.
75492
In a Bash script, I'm trying to store the options I'm using for rsync in a separate variable. This works fine for simple options (like --recursive), but I'm running into problems with --exclude='.*':  $ find source source source/.bar source/foo  $ rsync -rnv --exclude='.*' source/ dest sending incremental file list foo  sent 57 bytes  received 19 bytes  152.00 bytes/sec total size is 0  speedup is 0.00 (DRY RUN)  $ RSYNC_OPTIONS="-rnv --exclude='.*'"  $ rsync $RSYNC_OPTIONS source/ dest sending incremental file list .bar foo  sent 78 bytes  received 22 bytes  200.00 bytes/sec total size is 0  speedup is 0.00 (DRY RUN)   As you can see, passing --exclude='.*' to rsync "manually" works fine (.bar isn't copied), it doesn't work when the options are stored in a variable first.  I'm guessing that this is either related to the quotes or the wildcard (or both), but I haven't been able to figure out what exactly is wrong.
60409
I am looping through a number of files, and $file represents the current file.  How can I make copies or renames and keep the extension the same for this current file?  e.g. if $file = x.jpg  How to make a copy of $file's with a filename of x_orig.jpg  So far I have:  for file in /tmp/p/DSC*.JPG; do   cp $file $file+'_orig'.JPG done   but that copies the file  DSCF1748.JPG   to  DSCF1748.JPG+_orig.JPG   whereas I want a copy named  DSCF1748_orig.JPG   Similarly, using cp $file "$file"_orig.JPG  results in files such as DSCF1748.JPG_orig.JPG  I want to get the _orig in the middle of the filename...