touch files.txt; cat reorder_files.sh | while read line; do x=`echo $line | sed 's/touch \([a-z0-9\.]\+.*.pdf\);.*/\1/'`; echo $x >> files.txt ; done;

Get File Names from touch commands

retrieve file names back from touch commands for them
Sample Output
Input:
touch 01_intro.compressed.pdf; sleep 0.1;
touch 02_points.compressed.pdf; sleep 0.1;
touch 03_points.pdf; sleep 0.1;
touch lewis_pca_derivs.pdf; sleep 0.1;
touch shlens_pca.pdf; sleep 0.1;

Output:
01_intro.compressed.pdf
02_points.compressed.pdf
03_points.pdf
lewis_pca_derivs.pdf
shlens_pca.pdf

These Might Interest You

  • If you just try rm -i, the file name will be interpreted as a command line switch. Many commands let you use a double hyphen '--' to say 'No more switches, the rest are file names'. Incidentally having a file named '-i' in a directory where you use rm * will cause rm to act as if you specified '-i' on the command line. vim -- -v touch -- -t cat -- -c


    0
    rm -- -i
    merlinblack · 2011-07-26 04:00:32 0
  • This will make a false directory with the same file names as whatever directory you choose. This is wise to use when testing scripts that alter contents, filenames, or move files. I wrote this after an OOPS I made when renaming a directory of JPGs, PNGs, PSDs that were mixed. I recommend this as I lost over 2000 vacation pictures and some graphics I designed for software and web sites. :( NOTE: This only creates name copies, that data itself is not copied.


    1
    for each in /usr/bin/*; do echo $each | sed 's/\/usr\/bin\///' | xargs touch; done
    evil · 2011-10-13 19:14:34 0
  • Sometimes when copying files from one place to another, the timestamps get lost. Maybe you forgot to add a flag to preserve timestamps in your copy command. You're sure the files are exactly the same in both locations, but the timestamps of the files in the new home are wrong and you need them to match the source. Using this command, you will get a shell script (/tmp/retime.sh) than you can move to the new location and just execute - it will change the timestamps on all the files and directories to their previous values. Make sure you're in the right directory when you launch it, otherwise all the touch commands will create new zero-length files with those names. Since find's output includes "." it will also change the timestamp of the current directory. Ideally rsync would be the way to handle this - since it only sends changes by default, there would be relatively little network traffic resulting. But rsync has to read the entire file contents on both sides to be sure no bytes have changed, potentially causing a huge amount of local disk I/O on each side. This could be a problem if your files are large. My approach avoids all the comparison I/O. I've seen comments that rsync with the "--size-only" and "--times" options should do this also, but it didn't seem to do what I wanted in my test. With my approach you can review/edit the output commands before running them, so you can tell exactly what will happen. The "tee" command both displays the output on the screen for your review, AND saves it to the file /tmp/retime.sh. Credit: got this idea from Stone's answer at http://serverfault.com/questions/344731/rsync-copying-over-timestamps-only?rq=1, and combined it into one line. Show Sample Output


    5
    find . -printf "touch -m -d \"%t\" '%p'\n" | tee /tmp/retime.sh
    dmmst19 · 2012-11-05 20:32:05 2

  • 0
    find . -type f -exec touch "{}" \;
    kev · 2011-06-26 03:15:55 2
  • Useful for C projects where header file names must be unique (e.g. when using autoconf/automake), or when diagnosing if the wrong header file is being used (due to dupe file names) Show Sample Output


    0
    find . -type f |sed "s#.*/##g" |sort |uniq -c -d
    shadycraig · 2010-02-17 11:59:54 2
  • tstouch takes two arguments: a filename containing a timestamp, and an extended regular expression with the parenthesized section matching a timestamp of the form YYYYMMDDhhmm or YYYYMMDDhhmm.ss. It then touches the file with that timestamp. Show Sample Output


    2
    tstouch() { [[ $1 =~ $2 ]] && touch -t ${BASH_REMATCH[1]} $1; }
    bartonski · 2013-10-01 20:00:34 0

What Others Think

You have "data" in a fixed format. That seems to be a job for awk or sed. The delimiters are different: first space, then semicolon, so I'll use sed. . sed 's/^[^ ]\+ \([^;]\+\).*/\1/' input.txt > output.txt . sed s/search/replace/ . ^ beginning of line [^ ]\+ multiple NOT space characters (a space) \( \) remember this for later [^;]\+ multiple NOT semicolon characters .* the rest of the line \1 the text we found between the \( \)
flatcap · 109 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: