ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am fairly new to Linux, and would like to get into shell scripting using bash. A script which I would like to write, and would also actually use would be one that would change the file extensions on a bunch of files in a directory. However I can't figure out how to do this. Anyone have any ideas?
Hi Bob & Everyone!!!
Bob, I am very new to linux too so, I can't help you, but I wanted to ask everyone for the same type of help so I thought i'd just tag along on your thread. What I need to do is format the output of ls -l to change the User to 'me' and the group to 'my group', any files ending in .html to name (html file) and files ending in .c or .cpp to name (c source file). Also, if the date of the file is today's date it should say 'today'. Most of these things I can hack out, but i've saved them in a text file called 'd' and made this executable, but when I go back to my terminal it says there's no command called 'd'. I wondered if anyone knew why it would do such a nasty thing to me. I've specified the c shell as the one for it to run in & this is my default shell anyway...
I'm so frustrated. Any help would go a long way...
Here is a simple script that will rename files with one extension to another. This one is simple and quick, but it could easily be modified to be more versatile. You could add command line options like -o and -n for old and new extension, possible a -R to recursively search subdirectories. The command line can be parsed with the getopts command in a bash shell, and the ls can be substituted with a find to search recrusivly. Since this is a simple script it does have a few problems. If the extension you are searching for occurs more than once in the file, then it will replace the first one it finds.
Code:
#!/bin/sh
OLD_SUB="hpp"
NEW_SUB="h"
for FILE in `ls *.$OLD_SUB`; do
NEW_FILE=`echo $FILE | sed "s/$OLD_SUB/$NEW_SUB/"`
echo $NEW_FILE
mv $FILE $NEW_FILE
done
--As for Celly--
Sounds like 'sed' will be your tool of choice. Bash is much more powerfull when writing command scripts. csh is great for an interactive shell, but it lacks when writing scripts. Try executing your command file with . ./d
It is likely that a command inside the file is causing the error. If you were using bash, I would sugges that you run it in debug mode with: bash -x ./d
Thanks a mill. You're a pet. Just one ikkle question though 'cos I can't find out how to do this anywhere - like i've to change a few parts of the listing & I don't know how to group loads of 'sed' commands together...
You can group sed commands together by using a sed command file. Just put all the command in the file and specify the option to sed to read the commands from the file. Check the man page for the correct flag.
Rather than using the sed file I would start by spliting out each field of the ls -l command into a variable or array index seen below in two seperate examples. Once it is split out, you can modify each element as needed. To find out the file type you can use grep and check the $? (return value) to see if the pattern matched. Check out the man page. Replacing the date with today may require a seperate print statement or something else. One thing you also have to consider is when a symbolic link shows up in the listing.
Examples: (very incomplete, just for ideas)
Code:
#!/bin/sh
declare -a LS_ARRAY
for FILE in `ls`; do
ENTRY=`ls -l $FILE`
INDEX=0
for FIELD in $ENTRY; do
LS_ARRAY[$INDEX]=$FIELD
echo ${LS_ARRAY[INDEX]}
INDEX=`expr $INDEX + 1`
done
done
# Next Example:
ME=`whoami`
for FILE in `ls`; do
ENTRY=`ls -l $FILE`
PERMS=`echo $ENTRY | cut -d' ' -f1`
LINKS=`echo $ENTRY | cut -d' ' -f2`
USER=`echo $ENTRY | cut -d' ' -f3`
GROUP=`echo $ENTRY | cut -d' ' -f4`
SIZE=`echo $ENTRY | cut -d' ' -f5`
MONTH=`echo $ENTRY | cut -d' ' -f6`
DAY=`echo $ENTRY | cut -d' ' -f7`
YEAR_TIME=`echo $ENTRY | cut -d' ' -f8`
FILE_NAME=`echo $ENTRY | cut -d' ' -f9`
NEW_USER=`echo $USER | sed "s/$ME/me/"`
printf "%s %s %-8s %-8s %9s\n" $PERMS $LINKS $NEW_USER $GROUP $SIZE
# echo "$PERMS $LINKS $USER $GROUP $SIZE $MONTH $DAY $YEAR_TIME $FILE_NAME"
done
Bob and Celly, in the latest issue of Linux Magazine, Bill McCarty, author of Learning Linux from O'Reilly, has a column for newbies on shell scripting. You might want to check it out because in subsequent issues, he's going to elaborate a bit more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.