Sed to get folder name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed to get folder name
# 1  
Old 08-23-2006
Sed to get folder name

I need to write a shell script as follows:
1. Read the directory that contains 2 folders

> ls -ltr
total 60
drwxrwxrwx 2 abcusr abcgrp 512 Aug 01 10:31 20060801
drwxrwxrwx 2 abcusr abcgrp 512 Aug 02 10:30 20060802



2. Get the folder name with an early date (i.e. Aug 01 10:31)


I have written a script to get the first whole line of the containing the folder name, but I dont' know how to get the folder name only.

> ls -ltr | sed -n '/dr/{p;q;}'
rwxrwxrwx 2 abcusr abcgrp 512 Aug 01 10:31 20060801



Can anyone give me some advice, thx.
# 2  
Old 08-23-2006
Why not use awk? Use the '{print $NF}' command with awk.
# 3  
Old 08-24-2006
thx blowtorch. I am new in unix script. Could you mind showing me how to write the script, thx!
# 4  
Old 08-24-2006
here is another way of doing the same

Code:
ls -lrt | sed -n '/^d/{p;q;}' | sed 's/\(.*\) \(.*\)\//\2/'

# 5  
Old 08-24-2006
If I am not mistaken this is the way that blowtorch was explaining

ls -lrt | sed -n '/^d/{p;q;}' | nawk '{print $NF}'
# 6  
Old 08-24-2006
Quote:
Originally Posted by victorlung
thx blowtorch. I am new in unix script. Could you mind showing me how to write the script, thx!
How do you want to do this?
Code:
ls -ltr|awk '/^d/ {print $NF;exit}'

Code:
ls -ltr|awk 'NR==2 {print $NF}'

Code:
ls -ltr|awk '!/total/ {print $NF;exit}'

You could use any of these ways, and there are many more.
# 7  
Old 08-24-2006
torch,

either of the approach

Quote:
ls -ltr|awk 'NR==2 {print $NF}'
ls -ltr|awk '!/total/ {print $NF;exit}'
as mentioned
will display only the last column of the second line from the listing necessarily not to be a directory.. and that is not what OP had requested ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed command to replace "|" with ^ for all *.dat files in a folder not working

I am trying to use the below sed command to replace all "|" to ^, in a folder had 50 dat files. when i tried with 1 file it worked but when i tried with wild card, is not working. sed -i 's/"|"/\^/g' *.dat Is this the proper way to use sed command thank you very much for help. (3 Replies)
Discussion started by: cplusplus1
3 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

4. Shell Programming and Scripting

Replace character in files of entire folder? sed? or what?

Hello, I do have several files in one folder each file contains measurement data. for each file I would like to replace the character "," by "." ? How can I do this and how can I do this for each file at once? E.G. data_1.dat, data_x.dat (original version) data_1out.dat, data_x_out.dat... (10 Replies)
Discussion started by: rollinator
10 Replies

5. Shell Programming and Scripting

loop through files in each folder and perform sed

Dear all, I have few log folders in directory (FILE) and i need to perform sed on each files inside each folders. Here is my script, and i wish to rename each file back to the same file name after modification. Can anyone guide me on my script below? eg: folder1 contain files... (2 Replies)
Discussion started by: ymeyaw
2 Replies

6. Shell Programming and Scripting

sed to rename files in a folder - please help with script

Hello, I am new to shell scripting and stuck on renaming files in a folder. The files have the format chp01_00001.wav chp01_00002.wav .... chp02_00001.wav chp02_00002.wav .... but I want them to have the following names: chp_bloomy_00001.wav chp_bloomy_00002.wav chp_bloomy_00003.wav... (8 Replies)
Discussion started by: Bloomy
8 Replies

7. Shell Programming and Scripting

Replace last 2 folder directory string with sed

Hi guys, I´m trying to replace the 2 last folders name in a list of directories with a new string, but I´m don´t know which regex to apply. Directories list: C/my user/documents/games & music C/my user/documents/photos 09-24-2008 C/my user/settings/config ?1_2 * The last folder may have... (11 Replies)
Discussion started by: cgkmal
11 Replies

8. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

9. Windows & DOS: Issues & Discussions

How can I upload a zip folder on a unix path from my windows folder?

Hello, I am an amature at UNIX commands and functionality. Please could you all assist me by replying to my below mentioned querry : How can I upload a zip folder on a unix path from my windows folder? Thanks guys Cheers (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question