Truncate path and keep only filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate path and keep only filenames
# 8  
Old 09-24-2010
Quote:
Originally Posted by kicker75
I get this error message with the following line of code:

-sh: !.*/!!": event not found
That's an odd message, but since it wasn't going to do what you need, I'd say let it drop for now. I'm thinking on this; someone else may post some help before I do.

If French is your native language, then your English is great! It wasn't your fault -- I didn't see my misunderstanding until after I had posted.

---------- Post updated at 19:14 ---------- Previous update was at 19:09 ----------

Quote:
Originally Posted by kicker75
I can get the list I want with this line of code:

Code:
find /var/mobile/Applications/ -name "*.app" -printf "%f\n"

How to process each and create the directories from there? would for dir in something works? How to pipe the lists to the for dir loop?
Cool. Then this is an example of how you could make your directories:

Code:
find /var/mobile/Applications/ -name "*.app" -printf "%f\n" | while read file_name
do
        echo "making directory: $file_name"    # nice confirmation
        mkdir /var/stash/Themes/LoadingScreens.theme/Folders/$file_name
done

If you want to test it before you do any damage, change mkdir to echo, or add echo before mkdir and it will just print what it would do to the screen.

Hope this is finally of some use.

Last edited by agama; 09-24-2010 at 08:16 PM.. Reason: fixed typo
This User Gave Thanks to agama For This Post:
# 9  
Old 09-24-2010
Thank you very much! It worked!

This is what I used exactly:

Code:
find /var/mobile/Applications/ -name "*.app" -printf "%f\n" | while read file_name
do
        echo "making directory $file_name"    # nice confirmation
        mkdir "/var/stash/Themes.X7N9if/iMatte Loading Screens.theme/Folders/$file_name"
done

If I may ask, if I want to give that script to some people, knowing that their Themes.X7N9if will be something else, like Themes.Z49fP (always 5 characters), could I use Themes.????? instead, so it works with everyone?
# 10  
Old 09-24-2010
Quote:
Originally Posted by kicker75
If I may ask, if I want to give that script to some people, knowing that their Themes.X7N9if will be something else, like Themes.Z49fP (always 5 characters), could I use Themes.????? instead, so it works with everyone?
The problem with using wildcards is if there is more than one matching name it will cause problems on the mkdir command as it will expand to two pathnames.

I would look in /var/stash for Themes directories and if there is just one, use it, and if there is more than one, prompt the user to enter the desired target.

Something like this would work (untested!!)
Code:
tmp=/tmp/list.$$
ls -d /var/stash/Themes.* >$tmp 2>/dev/null     # look for existing directories

if [[ -s $tmp ]]     # if tmp file has size, something was found
then
        count=$(wc -l <$tmp)     
        if (( $count > 1 ))
        then
                echo "found more than one Themes directories in /var/stash; please enter path to use:"
                cat $tmp
                read tpath
        else
                read tpath <$tmp                # just get the one path
        fi

        tpath="$tpath/iMatte Loading Screens.theme/Folders"             # build the whole name
else
        echo "could not find a Themes directory in /var/stash [FAIL]"
        exit 1
fi


rm -f $tmp

You then can change your mkdir statement to be:

Code:
mkdir "$tpath/$file_name"

Sorry for the long response time -- got distracted.
This User Gave Thanks to agama For This Post:
# 11  
Old 09-24-2010
Works flawlessly! You are really good! Smilie

If you don't mind me asking one more thing, but before, here is a little explanation or what I'm trying to do now.

If someone is using my theme, prior to now, all the directories inside the loading screens directories (the .app directories), used to contain only one file, the loading screen, which was basically the same file.

As I added more and more Default.png (the file that goes there), it became clear I had to deal with this some other ways... having more than 500 loading screens, it was a bit more than 50 mb, just for the same file repeating itself.

So I created links instead, pointing to one file. I managed to search the net, and build a small script that removed the Default.png from each dir, and link instead to a Default.png located two folders up.

Now when I run the script on my iphone now, since I already ran it before, I get error messages saying cannot create, cannot link, bla bla bla.. that's normal, and doesn't matter, but would there be a clean way of dealing with this?

Also, would there be a way to determine, if there is a Default.png already there, to remove it (or even better, check if it's size is over a certain amount, like 1kb, so it's not a link but a real image, and if it's over 1kb, remove and link, and if under 1kb, skip it.)

The reason why I'm asking this, is because yesterday, when I first removed all the Default.png image and created links, I used this:

Code:
for dir in *;
do [ -d "$dir" ] && rm "$dir/Default.png" && ln -s ../../Default.png "$dir/Default.png" ;
done

And it worked pretty good. Except that when I wanted to add that part to the creating directories script you built me earlier, I ended up with messages saying there are no Default.png to remove, which is expectable(!), but it didn't followed on the linking part. So it worked and removed all the links, and created new ones, but all those that were just created and empty, they were still empty after too, no links got created.

So from now, I'd like to determine if there is already a .app dir in /Folders, if so, check if there is a Default.png, if not, create one, if there is one, check the size or link status, and if it's higher than 1kb, or if we can determine easily if it's a file or link, remove the file, and create a link, if it's already a link, skip it.

This would make updating the loading screens directories easier once you add more apps, and would also make it easier for people with different themes, to switch to a symbolic links based model.

I hope I got my ideas clear, and if you have any questions, let me know! I'll try to explain otherwise!

Thanks a lot for your fast and spot on answers! I really appreciate your help.

(offtopic) Do you have an iphone? If so, are you into theming? Please tell me, you might be interested in seeing what I have done!
# 12  
Old 09-25-2010
This will probably get you close enough. It is similar to the test and remove statement, but specifically checks and removes the .png only if it's a regular file, not a link. Then creates the link if it doesn't exist. I usually write scripts in Kshell, and am not sure what runs on the iPhone; I assume bash and generally Kshell scripts are compatible, but not always.

Code:
#!/usr/bin/env ksh
for dir in *
do 
        if [[ -d "$dir" ]] 
        then
                if [[ -f $dir/Default.png ]]            # true if exists and is an ordinary file; dont remove if link
                then
                        rm $dir/Default.png
                fi

                if [[ ! -e $dir/Default.png ]]          # true if it doesnt exist
                then
                        ln -s ../../Default.png $dir/Default.png
                fi
        fi
done

If this isn't exactly what you need, I think you'll be able to modify it. Hope it makes sense.

No, I don't have an iPhone. It's a neat platform, but honestly I want a something that I can put an external SD card into. I want to be able to manage the contents (music etc) on my own -- I don't want to be forced to use iTunes.
This User Gave Thanks to agama For This Post:
# 13  
Old 09-25-2010
Hi, this was almost spot on again! Only needed to add "" so the pathnames with space are okay!

The iphone uses standard sh. Also, you can manage your songs without itunes in multiple ways with the iphone, either using mediamonkey, a jailbroken app you can also install and will enable your iphone as a usb drive, and put songs directly in it, from any computer, mac or windows, with no software installed on the pc. Don't even need itunes installed.

And there is also another app on the pc you could use, transcopy, and I think some others too.

Here is my script now:

Code:
#!/bin/sh

tmp=/tmp/list.$$
ls -d /var/stash/Themes.* >$tmp 2>/dev/null     # look for existing directories

if [[ -s $tmp ]]     # if tmp file has size, something was found
then
        count=$(wc -l <$tmp)     
        if (( $count > 1 ))
        then
                echo "found more than one Themes directories in /var/stash; please enter path to use:"
                cat $tmp
                read tpath
        else
                read tpath <$tmp                # just get the one path
        fi

        tpath="$tpath/iMatte Loading Screens.theme/Folders"             # build the whole name
else
        echo "could not find a Themes directory in /var/stash [FAIL]"
        exit 1
fi


rm -f $tmp


find /var/mobile/Applications/ -name "*.app" -printf "%f\n" | while read file_name
do
        echo "making directory $file_name"    # nice confirmation
        mkdir "$tpath/$file_name"
done

find /Applications/ -name "*.app" -printf "%f\n" | while read file_name
do
        echo "making directory $file_name"    # nice confirmation
        mkdir "$tpath/$file_name"
done

cd "$tpath"

for dir in *
do 
        if [[ -d "$dir" ]] 
        then
                if [[ -f "$dir/Default.png" ]]            # true if exists and is an ordinary file; dont remove if link
                then
                        rm "$dir/Default.png"
                fi

                if [[ ! -e "$dir/Default.png" ]]          # true if it doesnt exist
                then
                        ln -s ../../Default.png "$dir/Default.png"
                fi
        fi
done

cd /var/mobile

I tried using for dir in "$tpath/*" or for dir in "$tpath" and it doesnt seem to run that part of the script. That's why I cd to it before, and then it works. I tried to replace one link with a file, and it successfully deleted it and linked instead.

Any other way I can use instead of cd'ing? And this time, no need to write it down for me, but just tell me, if I want to check if folder.app is there, I should nest a similar code you just created, but instead to look for a file, I should look for a dir?

Thanks!
# 14  
Old 09-25-2010
In this case cd'ing to the directory is probably the smart way. If you really wanted to be sure, you could test that the cd command worked before going on:

Code:
if ! cd $tpath
then
    echo "unable to switch to $tpath [FAIL]"
    exit 1
fi

Im not sure why putting it into the for loop directly didn't work, but Im tired and might be missing something obvious.

Yes, just add a check for the directory where you need it. Similar to the check for the file, but with -d.

Thanks for the scoop on the iPhone. I had assumed that iTunes was required and the fact that it can appear as a USB disk is wonderful. I might just consider one now!

Glad that you were able to get things working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

2. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

3. AIX

tprof, truncate the process path

Hi, i tryed tprof -skex sleep 6 but... Process PID TID Total Kernel User Shared Other Java ======= === === ===== ====== ==== ====== ===== ==== wait 57372 77863 31.31 31.31 0.00 0.00 0.00 0.00 wait ... (1 Reply)
Discussion started by: zanac
1 Replies

4. Shell Programming and Scripting

Sorting a list of filenames but keeping the path information.

Hi All I've googled around for this and can't see a way of doing it. I have a file that contains a number of records that are layed out something like the following. /path/to/directory/that/contains/a/file/I/need/filename.pdf The path itself can vary both in terms of the names and the... (7 Replies)
Discussion started by: Bashingaway
7 Replies

5. UNIX for Advanced & Expert Users

Truncate folder path

Hello, Given below are 2 sample paths from 2 different servers: /opt/temp/PROD/Script/New/Letters /opt/Share/temp/Share1/PROD/Script/Files/New/Letters I would like to truncate the path till the folder "PROD". Please note that the field count of the folder "PROD" vaires from... (1 Reply)
Discussion started by: DawnNish
1 Replies

6. Shell Programming and Scripting

Truncate table

Hi In unix able to connect to oracle database and create table ,when rerun ,if table exist ,truncate that table.Any idea how to do that a.sh ---- sqlplus -s datadmin/password <<EOF create table xx(col1 number, col2... ); exit; EOF I... (1 Reply)
Discussion started by: mohan705
1 Replies

7. Shell Programming and Scripting

Truncate File contain

I have one file which first line is blank and second line has some data. $cat filename output: 30-MAY-07 I want to store 30-MAY-07 value in one variable. for that I wrote var="`head -2 filename`" It will give that result but I want to truncate the first line which is blank. plz help. (2 Replies)
Discussion started by: rinku
2 Replies

8. Shell Programming and Scripting

Truncate directory path

Is it possibe to use sed for the following? I would like to truncate the output of a directory path if it's over 3 directory levels deep. For example: /dir1/dir2/dir3 -- NO change required but, /dir1/dir2/dir3/dir4 would output as ~/dir4 Thanks. (4 Replies)
Discussion started by: here2learn
4 Replies

9. Shell Programming and Scripting

How to truncate as filesize?

Hello everybody it's me again. I have a procces that is writing in a 'file1' automatically but i want to truncate 'file1' to a filesize 'x' that mean if the 'file1' size is 'x' i want to delete the first lines while the last lines are being writed, that have sence? in the process are an... (1 Reply)
Discussion started by: Lestat
1 Replies

10. UNIX for Dummies Questions & Answers

Truncate what is It?

what does this command do ? as in does this command just make sure everything in the file is executed? or does it flush the file? Actually this is used on a file in a progress database but I believe it is a unix command? (2 Replies)
Discussion started by: rocker40
2 Replies
Login or Register to Ask a Question