Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Practical questions about implementing a file naming convention

  1. #1
    Join Date
    Mar 2011
    Location
    South Dakota
    Beans
    1,134
    Distro
    Ubuntu 13.10 Saucy Salamander

    Arrow Practical questions about implementing a file naming convention

    Hi,

    When I got started using this machine with Linux I didn't always pay attention to the way I named directories and files. Some of them follow the naming convention I've decided to use but some have spaces or are otherwise not right. I know I tiny bit about using the command line but was hoping to get some guidance on the best way to accomplish a few, certain steps to bring all the personal directory and file names into conformity. I'll list what I think I need to do to reach that goal:

    I) Find all the files on my machine that are not in conformity with my chosen convention. The idea is to run some line of code that will build a big list for me. Not browsing through the whole system and making changes on by one.

    (I keep thinking - 'wildcards and/ or regular expressions with something like the find command or something like that - just don't know enough about it to form a real, practical, useful line of code to do the job. Or is that even the right tool for the job.)

    II) Separate from that batch all the directories and files that are not personal but are used by the system and eliminate them from my list. Also separate stored programs which filenames should not be changed.

    III) Somehow rename the entire lot in one massive shot to follow my desired convention.


    By the way, heres a description of the naming convention I want to use:

    ~ No spaces
    ~ Capitalize the first letter of every word
    ~ Use underscore only when absolutely necessary (that condition will pretty much always be when the captial letter beginning the next word is difficult to distinguish from the last letter of the previous word (such as a word beginning with captial I or L, etc when next to certain other letters. Occasionally, with longer file names, it is a logical consideration (such as grouping information in the name by type)).

    ~ One caveat is that some application names could not be changed. I don't mean things that are installed and in use but like some of the iso of Linux distros I have saved to mess with at some later date. I suppose I would reckon them like I do system files though.


    Is this something I can expect to be able to do with Linux? How, specifically, would I accomplish those goals?

    Thanks for taking the time to read this post. I appreciate any help.
    ----------------------------------

    Edit: Also, is there something I can set up on this machine that will enforce my new naming convention. Something that just won't allow you to name something outside of the convention, with some flexibility (like with underscore), and that will only enforce it with certain folders and files (like personal ones that I create)??

    Edit2: But then, if I'm successful in all that, I could potentially create other problems for myself as a result of renaming. Such as bookmarks that are already set.

    In additon to all this I need to also remove redundant folders. This often cones up with eBooks or other media I download. It gets downloaded in whatever way the person sharing it created the directory. Sometimes they put a folder instide a folder inside a folder or they also include items I don't want at all like other text files with links in them or whatnot.
    Last edited by ClientAlive; May 26th, 2011 at 03:05 PM.
    “ The best method for accelerating a computer is the one that boosts it by 9.8 m/s2. ”
    - Anonymous

  2. #2
    Join Date
    Sep 2007
    Location
    Oklahoma, USA
    Beans
    2,313
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Practical questions about implementing a file naming convention

    Here's a possible starting point for you:
    Code:
    jk@mehitabel:~$ find .|grep \ 
    ./Classen Alumni Bylaws-2.doc
    ./Documents/Getting Started with Ubuntu 10.04.pdf
    ./Public/pdf_gen/Foxit Reader.exe
    ./.mozilla/firefox/Crash Reports
    ./.mozilla/firefox/Crash Reports/InstallTime20101206122310
    ./.mozilla/firefox/Crash Reports/InstallTime20110323162424
    ./.mozilla/firefox/Crash Reports/InstallTime20101013121314
    There's a "space" character after that backslash in the "grep" command although you can't see it, and the list is only a small part of what I found when I ran this. You would really want to redirect the output to a file so that you can edit it.

    As a general rule you probably don't need to look at anything outside of your home directory and its subdirectories, because most things in other locations won't be owned by you and probably ought not be changed. You might even want to exclude all hidden files/folders in your home directory. A bit of experimenting is in order before doing anything that would make permanent changes.

    You could incorporate this into a script and use "sed" (stream editor) to make changes such as replacing the spaces with underscores. There's also a "tr" command that you could use to translate one character into another; I've not used it so can't give any good examples, but it's worth exploring...

    I'm not at all sure you'll find your new convention to be convenient or comfortable; personally I find switching to uppercase to be less convenient than replacing spaces with underscores, but it's a matter of individual likes and dislikes.

    Incidentally I'm amazed at how far you have progressed since we first corresponded in the forums here. Congratulations!
    --
    Jim Kyle in Oklahoma, USA
    Linux Counter #259718
    Howto mark thread: https://wiki.ubuntu.com/UnansweredPo.../SolvedThreads

  3. #3
    Join Date
    Mar 2011
    Location
    South Dakota
    Beans
    1,134
    Distro
    Ubuntu 13.10 Saucy Salamander

    Arrow Re: Practical questions about implementing a file naming convention

    Quote Originally Posted by JKyleOKC View Post
    Here's a possible starting point for you:
    Code:
    jk@mehitabel:~$ find .|grep \ 
    ./Classen Alumni Bylaws-2.doc
    ./Documents/Getting Started with Ubuntu 10.04.pdf
    ./Public/pdf_gen/Foxit Reader.exe
    ./.mozilla/firefox/Crash Reports
    ./.mozilla/firefox/Crash Reports/InstallTime20101206122310
    ./.mozilla/firefox/Crash Reports/InstallTime20110323162424
    ./.mozilla/firefox/Crash Reports/InstallTime20101013121314
    There's a "space" character after that backslash in the "grep" command although you can't see it, and the list is only a small part of what I found when I ran this. You would really want to redirect the output to a file so that you can edit it.

    As a general rule you probably don't need to look at anything outside of your home directory and its subdirectories, because most things in other locations won't be owned by you and probably ought not be changed. You might even want to exclude all hidden files/folders in your home directory. A bit of experimenting is in order before doing anything that would make permanent changes.

    You could incorporate this into a script and use "sed" (stream editor) to make changes such as replacing the spaces with underscores. There's also a "tr" command that you could use to translate one character into another; I've not used it so can't give any good examples, but it's worth exploring...

    I'm not at all sure you'll find your new convention to be convenient or comfortable; personally I find switching to uppercase to be less convenient than replacing spaces with underscores, but it's a matter of individual likes and dislikes.

    Incidentally I'm amazed at how far you have progressed since we first corresponded in the forums here. Congratulations!

    Well thanks. I've been working hard to learn.

    I didn't realize it but the next chapter in this eBook I'm reading (the one I'm in) is about regex (regular expressions). I think I'm starting to form and idea here (amazing, isn't it?).

    I'm thinking some combination of piping ls output into grep and then piping grep output into mv (if mv will take standard output like that).

    So the ls is to define the parameter as only the home directory. With the -R option it should include everything in it. That pipes into grep where I use regex to make it find filenames with spaces, dots, or whatever else I don't want them to have. That then pipes into mv which, if this is even possible, is given multiple directives, probably implementing regex again, to rename the files in the way I want them. When it gets to the mv stage maybe I can define my new names using . . . ? Well what I'm wondering about mv is whether I can define it's behavior rather than defining a file or directory name and having to enter names one by one. Maybe there is a better program for that (mv is a program right?).

    Of course I would test each part before the pipe, individually, to make sure I'm getting the right output and then run a trial in virtual box.

    Anyhoo . . . we'll see what happens. I've been playing around with it a bit but haven't really learned enough about regex or about more advanced ways to use mv. Not even sure mv works that way. If we figure it out I'll definitely post the solution for others who may be interested. It sure would be cool to be able to do all that in one fatal swoop like that. 5 min and and a lot less work as apposed to . . . God knows what if you had to search it all out yourself and rename things one by one.

    Next up on the chopping block, if we get this figured out, is to create some script or something that enforces the naming convention and won't let you name something outside of it's parameters in the first place.

    Wish me luck. Let me know if you have some good ideas.

    Thanks
    -----------------------------

    Edit: Oh! I had to re-read your post to be reminded of what you said about sed and tr. Maybe this tr thing works more like I was talking about anyway. Thanks man.
    Last edited by ClientAlive; May 27th, 2011 at 04:25 AM.
    “ The best method for accelerating a computer is the one that boosts it by 9.8 m/s2. ”
    - Anonymous

  4. #4
    Join Date
    Sep 2007
    Location
    Oklahoma, USA
    Beans
    2,313
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Practical questions about implementing a file naming convention

    Unfortunately the "mv" command doesn't take anything from stdin so it won't accept piped data. However scripting isn't all that difficult and can solve the problem easily. The trick is to use the advanced features in bash to execute a command or a pipeline, and store the result in a local environment variable. I don't know whether the book you are using has gotten to this stuff yet, though.

    Here's an illustration:
    Code:
    UNAME=$( uname -s )
    This executes the uname command to retrieve the system type, and stores it in the variable UNAME.

    A script can gather a list of all the files that meet your criteria, then read that list file by file into a pair of variables such as "oldname" and "newname", edit "newname" as required, and finally execute "mv $oldname $newname" to do the renaming before moving to the next file in the list. The beauty of this is that you can use "echo" instead of "mv" while developing the script, so it does not change anything but tells you exactly what it would be doing.
    --
    Jim Kyle in Oklahoma, USA
    Linux Counter #259718
    Howto mark thread: https://wiki.ubuntu.com/UnansweredPo.../SolvedThreads

  5. #5
    Join Date
    Apr 2011
    Beans
    484

    Re: Practical questions about implementing a file naming convention

    On that note, if your like me, your directories get stuffed with crusty files that you only need to access once a month or longer. The downloads directory is especially prone to this.

    You may want to look into automating file moving and sorting as well while your at it. (Especially if you have a consistent naming system.)
    Life is an extraordinarily long concatenation of luck and coincidence.

  6. #6
    Join Date
    Mar 2011
    Location
    South Dakota
    Beans
    1,134
    Distro
    Ubuntu 13.10 Saucy Salamander

    Arrow Re: Practical questions about implementing a file naming convention

    Hey guys. Oh my God! Sorry I went mia on ya' I just moved into a new place and my internet will not be transferred until the 3rd. I'm sitting in McDonalds right now to get internet access. Pathetic isn't it?

    “ The best method for accelerating a computer is the one that boosts it by 9.8 m/s2. ”
    - Anonymous

  7. #7
    Join Date
    Apr 2010
    Beans
    1,584
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Practical questions about implementing a file naming convention

    I'm all about the in-and-out burger man.. Don't get me wrong. Mickey Ds is ok, but. It ain't no double double with a strawberry shake.

  8. #8
    Join Date
    Mar 2011
    Location
    South Dakota
    Beans
    1,134
    Distro
    Ubuntu 13.10 Saucy Salamander

    Arrow Re: Practical questions about implementing a file naming convention

    Quote Originally Posted by linuxinstalledfromhdd View Post
    I'm all about the in-and-out burger man.. Don't get me wrong. Mickey Ds is ok, but. It ain't no double double with a strawberry shake.

    We don't have In-N-Out where I am but we do have Burger Time, love em'.


    As far as this whole file naming thing . . . I've about thrown my hands up on the whole thing for now. Like everything else in Linux it seems I've got another 9 mos of intense, round the clock, studying ahead of me before the baby is born. Nothing is ever just simple, is it? Wow!

    So I read and I practice and I get some ideas and I try them to see. My usual experience when trying new things on this machine is one of absolute, unrestrained terror. After what happened about a month ago I'm still pretty shaken.

    So I think, well I thought I found a way to find files with spaces in them and get them into a list. I tried:

    Code:
    ls -R /home/* | grep -RE '^([[:alpha:]]*[[:blank:]]*[[:alpha:]]* ?)$'
    and it seemed to bring up a list of things with spaces in them. Problem was, when I created a folder named: "blank space folder" in my home directory for a test and ran it again; that folder did not come up in the list. The first thing I thought of was maybe the info is coming from some database on the machine (like it does for locate) and it wasn't updated. I tried running:

    Code:
    ubdatedb
    and ran the test again (the first code in this post) but to no avail.

    I'm stumped. What's more, spaces in the filename is only one of a few things I need to search for. Even if I personally don't have any files named with certain, wrong, characteristics; it would be nice to develop something comprehensive that covers all possibilities of bad filenames.

    So that's where I'm at I guess. "tr" did come up in the book I'm looking at (in the same section I'm reading about POSIX) but I don't think sed has. I think those are useful for a later part of what I need to do though anyway. Right now I'm just trying to figure out how to build the list. Any thoughts?
    “ The best method for accelerating a computer is the one that boosts it by 9.8 m/s2. ”
    - Anonymous

  9. #9
    Join Date
    Sep 2007
    Location
    Oklahoma, USA
    Beans
    2,313
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Practical questions about implementing a file naming convention

    The locate database isn't involved at all with the "ls" command; that's a red herring.

    Your regular expression looks unduly complicated to me. It appears to be defining a line that begins with any number of alpha characters, followed by any number of blanks, then any number of alphas, then a single blank, a single character of any sort, and the end of the line. What happens if you eliminate the " ?" just before the ")"?

    Also, remember that "ls -R /home/*" is going to return more than just the list of file and directory names. Specifically, it will include header lines for each subdirectory! These may or may not match your regexp. Using "find /home" is more likely to create the kind of list you are looking for but the result will include full pathnames, not just the filename within each directory. This is a benefit, not a problem, because it eliminates possible ambiguities when you use the results to fill in a "mv" command.
    --
    Jim Kyle in Oklahoma, USA
    Linux Counter #259718
    Howto mark thread: https://wiki.ubuntu.com/UnansweredPo.../SolvedThreads

  10. #10
    Join Date
    Mar 2011
    Location
    South Dakota
    Beans
    1,134
    Distro
    Ubuntu 13.10 Saucy Salamander

    Arrow Re: Practical questions about implementing a file naming convention

    Quote Originally Posted by JKyleOKC View Post
    The locate database isn't involved at all with the "ls" command; that's a red herring.

    Your regular expression looks unduly complicated to me. It appears to be defining a line that begins with any number of alpha characters, followed by any number of blanks, then any number of alphas, then a single blank, a single character of any sort, and the end of the line. What happens if you eliminate the " ?" just before the ")"?

    Also, remember that "ls -R /home/*" is going to return more than just the list of file and directory names. Specifically, it will include header lines for each subdirectory! These may or may not match your regexp. Using "find /home" is more likely to create the kind of list you are looking for but the result will include full pathnames, not just the filename within each directory. This is a benefit, not a problem, because it eliminates possible ambiguities when you use the results to fill in a "mv" command.

    If I run:

    Code:
    ls -R /home/* | grep -RE '^([[:alpha:]]*[[:blank:]]*[[:alpha:]]*)$'
    I get (well I guess I can post this):


    Code:
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dira
    dirb
    dirc
    dird
    dire
    dirf
    dirg
    dirh
    diri
    dirj
    dirk
    dirl
    dirm
    dirn
    diro
    dirp
    dirq
    dirr
    dirs
    dirt
    diru
    dirv
    dirw
    dirx
    diry
    dirz
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    John Schember
    Unknown
    William Shotts
    
    
    
    
    
    
    
    
    
    
    
    Hack Central
    
    Usenet
    
    
    
    Hubble
    Photos
    RolledTheVan
    Yar
    All the "dir . . ." that are listed are inside a folder called "cliPlayground" that I created to practice learning code in a relatively safe environment.

    And if I run:

    Code:
    ls -R /home/* | grep -RE '^([[:alpha:]]*[[:blank:]]*[[:alpha:]]* )$'

    I merely get a new line (uname@host) with, apparently, no results.


    I tried the "find /home" thing; and, of course, get the listing of the entire home directory - which is cool. I'll have to work on how to incorporate something for a more specific search later today. I have to run for now.

    Thanks for the info Kyle


    Jake
    “ The best method for accelerating a computer is the one that boosts it by 9.8 m/s2. ”
    - Anonymous

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •