Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 11-25-2011, 11:08 AM   #1
Registered: Feb 2005
Location: Calgary, AB Canada
Distribution: Kubuntu 6.10
Posts: 44

Rep: Reputation: 15
Actual file order in a directory

I have a TV that will do a slide show of photos from a USB device. I have a directory of my recent vacation photos with a sub-directory for each location we visited. The photos are properly in order by file name in Dolphin, Digikam and Gwenview. When I copy this directory structure to a USB device and show it on the TV the photos are not in order by file name. I have done research and found that this is a common problem in many devices like TVs and mp3 players. The displayed order and the physical file order in a directory in Linux are not the same. I did find a script called findalpha that stated it would correct this issue by creating a temporary directory and copying files one-by-one, in file name order, and then back to the original directory so that the actual order and file name order match. It doesn't work on my Kubuntu 11.10. The jpg files are still not in file name order on the USB device. I can't seem to locate any other solution. Has anyone else solved this problem?
Old 11-25-2011, 11:28 AM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
You may need the file names to start with a number. For example, my DVD player expects that for pictures and mp3 files; such as

The order given in a graphical file manager doesn't indicate any order in a directory. You can change how files are sorted, name, date, etc. Either by clicking on the column header, or by an option in the menu.

If the order the TV sees reflects the order they were copied, you could copy the files to the USB pendrive in the shell. The wild card, e.g. cp *.jpg /media/usbdrive/, will result in the jpeg files being sorted and copied in that order.

Last edited by jschiwal; 11-25-2011 at 11:32 AM.
Old 11-25-2011, 12:02 PM   #3
Registered: Feb 2005
Location: Calgary, AB Canada
Distribution: Kubuntu 6.10
Posts: 44

Original Poster
Rep: Reputation: 15
The idea of starting with a number did not work for me. However, the use of cp *.jpg /media/'stick name', appears to have worked. Now I need to develop a bash script that will do this recursively down the directory structure. Thanks for the tip.
Old 11-25-2011, 05:44 PM   #4
Registered: Mar 2008
Posts: 19,428

Rep: Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999
I think the order is ls -f
Old 11-27-2011, 09:07 AM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,845

Rep: Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006
Check out fatsort. It's probably already in your distro's repositories.

Also, if you would post the script you have, and whatever error messages/output you get, we may be able to figure out why it isn't working.

Finally, note that names are generally sorted in ascii string order (or the order of the locale your system is set to), rather than numerically. So 10 will usually come before 2. If this is a problem you may want to zero-pad the numbers first.

I've written a script that will automatically pad the numbers in filenames:


# Pads numbers in file names if found.
# See help message for more.

# Set the environment
shopt -s extglob

BCYAN="${BCYAN:-\e[1;36m}"     #Define some color codes, for prettified output.
#BGREEN="${BGREEN:-\e[1;32m}"  #Uses environment defaults if they exist.

#set the default padding level

# Set up the help dialog:
help+=( '' )
help+=( '\tA quick script to zero-pad files that contain numbers.' )
help+=( '\tIt will only pad the first number string it finds in the name, and ignores files without numbers in their names.' )
help+=( '' )
help+=( "\tUsage: \t${BCYAN}${0##*/} [-n <num>] <files>${RESET}" )
help+=( "\t\t${BCYAN}${0##*/} -h${RESET}" )
help+=( '' )
help+=( '\tUse -n to specify the number of digits to pad, from 1-9.  Defaults to '"$pad"' if not used.' )
help+=( '\tIf no files are given, it processes the current directory.' )
help+=( '' )

# Process input options
# If "-h" print help & exit.
# If "-n" test for valid inputs and set "pad" variable
# Ignore anything else
while getopts ":hn:" opt; do

	case "$opt" in

		h)	IFS=$'\n'
		echo -e "${help[*]}" >&2
		exit "2"

		n)	if [[ "$OPTARG" =~ [^[:digit:]] ]] || (( "10#$OPTARG" < 1 )) || (( "10#$OPTARG" > 9 )); then
				echo -e "${BCYAN}invalid option: [$OPTARG].${RESET}" >&2
				echo -e "${BCYAN}-n must be an integer from 1 to 9${RESET}" >&2
				echo -e "${BCYAN}Falling back to the default of $pad${RESET}"
				pad="$(( 10#$OPTARG ))"

		\?) echo -e "${BCYAN}Invalid option: [-$OPTARG].  Ignoring.${RESET}" >&2

shift $(( OPTIND - 1 )) ; OPTIND=1

# Now test for input files.
# If nothing given, set input parameters to files in current directory.
if [[ -z "$*" ]]; then
	set -- ./*

# Process files in input parameters
for file in "$@" ; do

	# Ignore files without digits
	[[ "$file" != *[0-9]* ]] && continue

	# Split filename into prefix-digits-suffix
	[[ "$file" =~ ([^[0-9]*)([0-9]+)(.*) ]]

	# Pad digits to desired width
	printf -v numpad "%0*d" "$pad" "${BASH_REMATCH[2]##*(0)}"

	# Add old and new filenames to arrays for final processing
	oldfile+=( "$file" )
	newfile+=( "${BASH_REMATCH[1]}${numpad}${BASH_REMATCH[3]}" )


# If there are any files to rename, ask to confirm the operation.
# And rename if confirmed.
if [[ -n "${oldfile[*]}" ]]; then

	echo -e "${BCYAN}Rename the following files?${RESET}"	
	for i in "${!oldfile[@]}" ; do
		echo -e "${oldfile[i]/#$PWD/.}\t-->\t${newfile[i]/#$PWD/.}"
	read -p "(y/n): "

	case "$REPLY" in

		y|Y*)  	for i in "${!oldfile[@]}" ; do
					mv -n "${oldfile[i]}" "${newfile[i]}"

		*)		echo -e "${BCYAN}Aborting.${RESET}"
				exit 1

# Otherwise just exit.
	echo -e "${BCYAN}No files to rename.${RESET}" >&2
	echo -e "${BCYAN}Exiting.${RESET}" >&2
	exit 1

exit 0
Old 11-27-2011, 10:53 AM   #6
Registered: Feb 2005
Location: Calgary, AB Canada
Distribution: Kubuntu 6.10
Posts: 44

Original Poster
Rep: Reputation: 15
I will try the fatsort idea. I assumed that fatsort was a windows thing when I saw it. I didn't read deep enough. The rename feature of Gwenview and Digikam does a good job of providing zero-filled, incremental, numbered names as well.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to restore the actual file from my_file.c,v file archieval Linux - Software 2 06-15-2011 03:32 PM
bash: renaming file extension based on actual file type alekone Linux - General 9 12-28-2008 10:12 AM
cannot stat ... "modules.order" ... No such file or directory shogun1234 Linux - Kernel 1 11-27-2008 03:12 PM
How to rsynce the actual directory path instead of just the directory at the end of Niceman2005 Linux - General 5 04-08-2005 08:58 PM
Getting actual directory name subu_s Programming 6 01-13-2005 08:59 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration