Sunday 3 March 2019

How to organize files in accordingly named directories from command line under Linux


I have a directory filled with various files, differently named (i.e. not named with a specific pattern) and with different extensions, and I want to put each file in a newly created subdirectory named after each file; what's the best way to accomplish that from the command line under Linux?
This sounds a bit confusing, let me make and example: let's call a random file in the directory ./filename.ext, I'd like to put it in a subdirectory called ./filename so that effectively the new path will be ./filename/filename.ext.


I did some trials with the following steps:



  • I've created some test files, from file-1.txt to file-9.txt, inside a test directory with for num in {1..9}; do touch file-$num.txt; done

  • I've created a directory named after each filename with for name in $(eval ls | cut -b 1-6); do mkdir $name; done

  • I've moved the source files in their respective directories with for name in $(eval ls | grep -e ".txt" | cut -b 1-6); do mv $name.txt $name/$name.txt; done


As you can see, this solution works, but its huge drawback is that it works just in this case, with files of that extact filename lenght and with that particular extension. If I have some files with different names and extensions, this would be of no use at all. The use of cut command is not the ideal solution here, I suppose, but I haven't found anything better at the moment.


As a bonus, it would be cool to find a way to create the subdirs automatically when moving/renaming the files, without having to use mkdir previously (so when I ls to actually rename, I don't have to worry about excluding the newly created directories); but in the end this is a secondary problem.



Answer



I guess the solution to your problem is called parameter substitution (have a look at the detailed description). Given a name of a file in filename you'll get its extension using ${filename##*.} and the name w/o extension with ${filename%.*}.


That said, you may want to modify your for loop like:


# for all (*) files in the current directory
for i in *;
do
# -f checks if it is a file (skip directories)
[ -f "$i" ] || continue
# store the file name in filename
filename=$(basename $i)
# ext = the extension of the file (here we don't care, jfi)
ext="${filename##*.}"
# dir = the file name without its extension
dir="${filename%.*}"
# create the directory
mkdir -p $dir
# move the file in that directory
mv $i $dir
done

Unfortunately, I did not get the point of your bonus task, so it'll remain an open challenge... :)


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...