Results 1 to 3 of 3
Hello All,
I am now have a question about how I can browse to different directories in a fast and convenient way.
Suppose I am at the root directory and ...
- 08-08-2011 #1Just Joined!
- Join Date
- Aug 2011
- Posts
- 1
Browse to different directories by script
Hello All,
I am now have a question about how I can browse to different directories in a fast and convenient way.
Suppose I am at the root directory and I have 1000 sub-directories named "000", "001", ..., "999". Now I want to traversal each sub-directory, basically what I need to do is:
cd 000
cd ..
cd 001
cd ..
...
cd 999
cd ..
It is very easy to implement in Matlab, but I have no idea how I can do the same in the linux script, do you have any suggestion?
Regards,
Greene
- 08-08-2011 #2
First: Placing 1000 directories in / is not a good idea.
You might want to use a data directory, for storage/performance reasons, but also not to clutter your system.
Then there is numbering.
Imho, it is not a good idea to pad with leading zeros.
a) loop constructs are harder, as you need to work with temp variables and printf
b) if you ever need more than 999 dirs, then you would need to rename 001 - 999 to 0001 -0999
Say, the dirs would be
/data/1
/data/2
etc
then in bash you can do somehting like this:
It uses absolute paths, so you dont need the cd ..
which are half of your calls
Code:DATADIR="/data/" for a in {1..999}; do cd ${DATADIR}/$a done
Additional note:
Why do you need to cd in the first place?
A script/application should be able to work no matter from where it is called.
Or the other way around:
Anything (e.g. paths) it needs is either given as argument or in a config file.You must always face the curtain with a bow.
- 08-08-2011 #3Linux Newbie
- Join Date
- Dec 2009
- Posts
- 241
If you don't know what directorys exists and you wanna do something in every directory ... or in directorys with a special name or anything you can also use following:
Code:DATADIR="/data/" find $DATADIR -maxdepth 1 -mindepth 1 -type d | while read FOLDER ; do cd $FOLDER echo "$FOLDER visited" done


Reply With Quote