User Tools

Site Tools


Manage your working environment with file commands

Use this wiki to get familiar with file commands useful for managing directories and files.

pwd ls cd mkdir commands

First we need to create a new folder in the
~/ost4sem/exercise/basic_adv_bash directory and call it example_manage

pwd # Which is our working directory ?
ls # Which files exists in our working directory?
ls -l # list with detailed info
cd ~/ost4sem/exercise/basic_adv_bash # change working directory
ls -l
mkdir example_manage
ls -l
rm -r example_manage # We remove the directory we just created

We can also create a folder or copy a file in a target directory from a different working directory

mkdir ~/ost4sem/exercise/basic_adv_bash/example_manage
ls ~/ost4sem/exercise/basic_adv_bash/

Loops for multiple folder and files management

Now we need to create a set of working directories for different years (2000 to 2010) each one with 12 subdirectories corresponding to months (Jan to Dec).

Year="Yr2000 Yr2001 Yr2002 Yr2003 Yr2004 Yr2005 Yr2006 Yr2007 Yr2008 Yr2009 Yr2010"
month="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
for y in $Year ; do  mkdir $y
for m in $month ; do mkdir $y/$m ; 
done ; 
ls */*/

Now we create a text file

echo This is my exercise for learning bash scripting $Year $month >> example_text.txt 

And we copy this text file in every month for the years 2000 to 2010 folders we just created

for y in $Year ; do 
for m in $month ; do
cp example_text.txt $y/$m/copy_of_text.txt ;
done ;
ls */
ls */*/

Difference between cp and mv

Now en need to rename the exampletext.txt as Year2001.txt and move it from the ~/ost4sem/exercise/basicadvbash/examplemanage folder to ../example_manage/Yr2001/ folder

mv example_text.txt ./Yr2001/Year2001.txt
ls Yr2001

Now we copy the Yr2001.txt file to the Yr2000 folder and name it copyofyear2001.txt

cp ./Yr2001/Year2001.txt ./Yr2000/copy_of_year2001.txt

Now we delete the folder example_manage and all its content

rm -r ~/ost4sem/exercise/basic_adv_bash/example_manage
wiki/managedirbash.txt · Last modified: 2018/06/01 11:09 (external edit)