Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi all !! This is homework by the way, and I have just started linux/unix. I have to append the string 'UNIX_' to the beginning of all file names in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2012
    Posts
    11

    Append 'UNIX_' in the beginning of all file names in a folder


    Hi all !!

    This is homework by the way, and I have just started linux/unix.
    I have to append the string 'UNIX_' to the beginning of all file names in a folder.
    Ex: If I have abc.txt and xyz.doc, they should be renamed to UNIX_abc.txt and UNIX_xyz.doc. I know I can do this with a loop, but it specifically says 'with a single command'.

    I read about the rename command, but I dont know how to use it here.

    I tried a few things, but none of them work:

    rename * UNIX_* *.*
    rename [a-zA-Z0-9_.] UNIX_(??) *.* (I dont know what "??" should be)

    Need some hints !! (Is there a better command?)


    Thank you in advance !!

  2. #2
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Read the rules.

    3. no religious posts, political posts, homework questions, or classified ads

  3. #3
    Just Joined!
    Join Date
    Mar 2012
    Posts
    11
    Oh !
    I guess I shouldn't have been honest, then. :P

    Thanks anyway !

  4. #4
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Anyway, by "1 command only" I would assume a loop is considered a single command, probably just means the entire process should be automatic rather than needing to create a separate statement for each file.
    Just ask your teacher for some clarification on that if you need it, no such thing as a stupid question.

  5. #5
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    It's good to be honest, we just won't give you a direct answer. My hint to you is this:

    The "find" command lets you search for files and execute a command on every file using the "-exec" parameter. Learn how to use the "find" command and it's "-exec" parameter. If you can get "find" to execute "rename" on every file it finds, this will solve your problem.

    There are other ways to solve this problem, but the "find" command is probably the simplest way.

  6. #6
    Just Joined!
    Join Date
    Mar 2012
    Posts
    11
    Thank you so much!

    I read about the find command, and it solves half my problem. But my problem still remains with the rename command.
    For example, if I told you that in the directory home/files/ there is ONLY 1 file (but you dont know its name), then how would you use rename command to rename it to UNIX_(filename). I can only use rename to replace some text in filenames with some other text (like .htm to .html, etc.), .i.e I already know some part of the filename I want to rename. But here the filename can be anything, so I cant understand how to use it here. (For example, if the question was to rename all fles with abc in them to UNIX_abc, then I could have done that)

    rename * UNIX_* * doesnt work (obviously, its too weird). When find finds a file, can I make it put the filename in a variable or something? Then I could do it like this:

    Code:
    find . -[option-to-put-filename-in-variable] var -exec rename var UNIX_var * \;
    OR If I can use a regex in rename itself, something like below (which doesnt work)

    Code:
    rename '/s/*/UNIX_*/'; *
    or like this:
    Code:
    rename '/sp{M}/*/UNIX_$M/'; *
    , then problem is solved. Any idea about regexes or how I can use rename to rename files whose name I dont already know (or know a part of name)?

    Thanks for all the help !!

  7. #7
    Just Joined!
    Join Date
    Mar 2012
    Posts
    11
    Oh hey I got the answer. Had to learn a bit of perl regexes though.
    Code:
    rename 's/^/Unix_/' *
    And the command rename does come under UNIX, right? (Its a unix assignment, but I am working on linux. I guess they're almost the same, but want to confirm.)

    Thanks a lot !

  8. #8
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    The "find -exec" command is intentionally very limited (so it can limit the damage you do if you make a mistake). One possible way to solve your problem is to write a script program like "my_rename.sh" which contains one line of code, the "rename" command, and then use "-exec" to pass every file name you find as a parameter to this script.

    However, let me tell you of another technique I use very often. It is pretty advanced, but I think everyone should learn this technique.

    You can write a loop that reads one line of data at a time from it's input. To do this, you can use the pipe operator "|" to pipe output from "find" to use as input to the loop. Take a look at this:
    Code:
    find ./my-files | while read FILENAME; do echo $FILENAME; done
    Try executing that line of code, see what it does. What happens is, find produces a list of files, one file per line, then the "while read FILENAME" part reads every line (which contains a file name from "find") and then for each line, the "$FILENAME" variable will become the name of every file it reads from find.

    Then, key is the part I highlighted in green. I used "echo" as the command, so as not to give away the answer. However, if you experiment around with that green part between "do" and "done" you will begin to see the possibilities. Instead of echo, you can use tools like "basename" and "dirname", and the back-quote operators (e.g. `basname $FILENAME`) to fine-tune exactly how that green bit of code works. This will let you precisely control how every file name is treated, and if you are clever, you may be able to figure out exactly how to solve your problem.

    Be careful, you can accidentally overwrite files, so make backups of the directory you are experimenting with. Also, before you try a command like "rename $X $Y", try doing "echo rename $X $Y" first so you can see what command will run, that way you don't accidentally end up doing something you didn't expect.

  9. #9
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Quote Originally Posted by hypernova View Post
    Oh hey I got the answer. Had to learn a bit of perl regexes though.
    Code:
    rename 's/^/Unix_/' *
    And the command rename does come under UNIX, right? (Its a unix assignment, but I am working on linux. I guess they're almost the same, but want to confirm.)

    Thanks a lot !
    OK, you are using Perl commands. I thought you were using Bash commands.

    Technically, Linux and UNIX are completely different, but the user interfaces for Linux and UNIX are almost identical; they both use the "Bash" shell for executing commands.

    "Bash" is a command line user interface software that runs well on both Linux and UNIX. So as long as your UNIX system and Linux system are both using "Bash," then you should be OK, the code you write for one will work on the other 99% of the time. There are some slight differences that might cause you trouble. For example, Linux uses the GNU version of the Mv utility command, but some UNIX systems do not, they use a BSD version of Mv. One difference, for example, is that GNU Rename provides you with the "-t" parameter, but the BSD Rename does not. So depending on the course you are taking, you may not be able to use "-t" as a parameter to the "mv" command to solve your homework problem, even though it may work in Linux.

    Also, not all UNIX systems use Bash. Bash is a GNU software. Older UNIX systems used Tcsh, or the proprietary Ksh. This is changing now, Bash is pretty much everywhere, even on UNIX. But again, it depends on the class you are taking. Tcsh and Bash are pretty different, though both are command line user interfaces. To a beginner, the commands you write in Tcsh look the same as the commands you write in Bash, but more advanced commands are NOT compatible. Be sure your course work assumes you are using Bash instead of something else like Tcsh before continuing using Linux to do course work.

    Perl provides extensions to the command line interface, including Rename, and Perl also works identically on both UNIX and Linux.

  10. #10
    Just Joined!
    Join Date
    Mar 2012
    Posts
    11
    Thank you very much for your reply !!

    Your earlier post was extremely useful as I am new to scripting (but I know C/C++). I knew it could be done with a loop (I was thinking ls instead of find), but not exactly how. Now I understand a few more things, thanks to you. The reason I did not want to use that answer was that the question specifically mentions that I have to use a single command, and I don't know, a loop doesn't look like a single command (semicolons). (its like a series of commands forcibly put in one line, but I am not sure). I have no preference for perl or bash commands (I am new to both!) its just that for this question because of "use single command" clause, perl's answer seemed more satisfactory(as long as it can be submitted in a UNIX assignment).
    Actually I am doing compulsory online training before joining a company, and have to submit assignments. The name of the assignment is UNIX assignment -1, I know nothing more, and don't have any teacher or coursework to clarify things with.

    Thanks !!

Page 1 of 2 1 2 LastLast

Posting Permissions

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