Issue : The code works fine, it lists folders with "numerical names" in a reversed order, however I would have to be in the same directory. I would like to add a custom path to it so that I do not have to be in the same directory.
ls -d */ | cut -d '/' -f 1 | sort -nr
I tried adding " /test/path
" to the code above, but without success.
ls -d */ /test/path/ | cut -d '/' -f 1 | sort -nr
Update : I do not know why "test2" is being printed at the bottom in this scenario. I am using SliTaz Linux 5.0
I can not figure out the shell
version.
root@s1:/# mkdir /test
root@s1:/# mkdir /test/test2
root@s1:/# cd /test/test2/
root@s1:/test/test2# mkdir 1 2 3 10
root@s1:/test/test2# touch test.txt
root@s1:/test/test2# ls -d /test/test2/*/ | awk -F'/' '{print $(NF-1)}' | sort -nr
10
3
2
1
test2
root@s1:/test/test2#
Update 2 : On my cloud server which uses "Alpine Linux" version "3" I believe, there does not seem to be a problem, the code works ok.
test:/# mkdir /test
test:/# mkdir /test/test2
test:/# cd /test/test2
test:/test/test2# mkdir 1 2 3 10
test:/test/test2# touch test.txt
test:/test/test2# ls -d /test/test2/*/ | awk -F'/' '{print $(NF-1)}' | sort -nr
10
3
2
1
test:/test/test2#
ls -d /test/path/*/ | cut -d '/' -f 1 | sort -nr
Although cut
won't work the way you intend to use it.
Instead use this:
ls -d /test/path/*/ | awk -F'/' '{print $(NF-1)}' | sort -nr
Also to avoid nullglob, set this before:
shopt -s nullglob
So that if no directories exist, it doesn't result into error.
I am not sure about the reason the above command doesn't work on SliTaz Linux 5.0
, but as discussed, this can be done instead:
cd /test/path/ ; ls -d */ | awk -F'/' '{print $(NF-1)}' | sort -nr ; cd "$OLDPWD"
or
(cd /test/path/ ; ls -d */ | awk -F'/' '{print $(NF-1)}' | sort -nr)