Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've got a USB drive with mp3 music on it that I play in my car. My car has a USB drive port. Problem is that the car sorts the music by Creation/Modification Date (and not alphabetically). This makes it a pain to find what artist I'm looking for.

To solve this problem, I need a way to "update" the Creation/Modification Date for all folders on the USB drive every time I add a new folder of music to the USB drive. Anyone know how I can do this?

The Touch command works great, but the format touch <filename> would take forever to do on each folder on the drive. Anyway to select all folders on the drive and then touch <all folders>?

On my USB drive I have a folder for each album and the songs within each respective folder, like so: Album-1-folder Album-2-folder

I need to apply the Touch command to each of the "album" folders on the drive.


I stumbled upon a simple solution to solve this issue:touch /media/USB_Drive/*

share|improve this question

3 Answers 3

If you have a lot of files to touch, it's just going to take a while. You can plug-in the drive, and in a terminal, assuming your drive is mounted as /media/USB_DISK, do the following:

for i in `find /media/USB_DISK -type d|sed -e 's/ /%20/g'|sort`; do
   echo $i|sed -e 's/%20/\\ /g'|xargs touch
done

Just replace the USB_DISK with the proper name of the folder where your drive is mounted under the /media/ directory.

share|improve this answer
    
I don't understand what this command is doing. "for i in find"? Would you mind explaining briefly what this is doing? –  Chad--24216 Jul 2 '12 at 19:32
    
Ah, sorry. askUbuntu ate the formatting apparently, there are supposed to be backtick quotes (pressing the ~ key without holding shift), around the "find /media/USB_DISK -type d|sort" piece. This will find all directories underneath /media/USB_DISK, loop through them, echo the directory name to the console (so you can see it), and touch the directory. –  dobey Jul 2 '12 at 19:34
    
could u try editing the format to make it accurate? thx dobey. –  Chad--24216 Jul 2 '12 at 19:38
    
I've inserted [backtick] where the ` character should be, but the ; is also apparently treated specially, so I can't seem to get it formatted in multiple lines correctly either. –  dobey Jul 2 '12 at 19:53
    
@dobey just use a backslash before the backtick to escape it. –  hexafraction Jul 2 '12 at 20:00

Dobey's answer is on the right track, but it will fail if any directory name contains a space. In general, it's not a good idea to use the output of find in a script for the same reason that the output of ls shouldn't be parsed by a script: File names can easily contain characters which make it difficult or impossible for a script to accurately interpret.

There are several options here. One is to use the -print0 switch to find combined with an appropriate call to xargs. See the respective man pages for details.

The approach I'm recommending is simpler for the specific problem at hand and uses shell globing to get accurate file names:

for i in /media/USB_Drive/*; do
  [[ -d "$i" ]] || continue   # skip non-directories
  echo "touching $i..."
  touch "$i"
done

Note that using a glob automatically results in an alphabetized list.

share|improve this answer
    
This only works for files in the top level tree though, and not any sub-directories which might exist. –  dobey Jul 3 '12 at 16:56
    
@dobey Well, it's likely only one or two levels, so touch /media/USB_Drive/*/ and touch /media/USB_Drive/*/*/ will probably suffice. –  geirha Jul 3 '12 at 17:16
    
@dobey: It's true that it's limited in scope. A more thorough approach would be to write a recursive function. But note that in general, globbing is still the way to go here. –  Scott Severance Jul 3 '12 at 21:12

Even if you get that to work, all files would have the same date. It doesn't sound like that would help.

What would work is a script that has hard-coded file names. Hard-coding the file names is necessary to create your preferred order. Just "touching" them in the preferred order will "create" your order, so you don't need to enter actual dates into the touch command.

#!/bin/bash

cd /path/to/folder1
touch file1
sleep 1
touch file4
sleep 1
touch file2
sleep 1
touch "file 5 with spaces"

cd /path/to/folder2
touch file9
sleep 1
touch file3
sleep 1

The "sleep" commands are needed to force each "touch" to have a different time. Note that the file "numbers" don't matter, and are processed out of numerical order. The order in the script, however, creates the "processing" order.

Note that you will need to update and run your script every time you add new songs/files.

Edit: I just saw the "sorted find" post. That would probably work better. :)

share|improve this answer
    
same date is what I need. If the date is the same, the alphabetical listing that the file-browser had is how the folders show up in the car. So I'm looking for a way to simply make all folders have the exact same date after I add a new folder. –  Chad--24216 Jul 2 '12 at 19:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.