Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sep 12, 2011

How to rename multiple file extensions with bash

Imagine you have some logfiles with .txt extensions and you want them all to be .log files.
You just need to type this within the logfile directory:

for file in *.txt; do mv $file ${file%.txt}.log; done


There many situations where renaming many files very easly is interessting.

Feb 5, 2011

How to do something 10 times with bash

There is a command called seq (Sequence).
seq takes three parameter. The first parameter is the startnumber. The second is the step the start number will increment with until it reaches the third parameter. The endnumber.

seq 1 1 10 gives an array like: 1 2 3 4 5 6 7 8 9 10
seq 1 2 5 gives you: 1 3 5

If you want to do something 10 times you can use a for loop:

for i in `seq 1 1 10`
do
echo "hello $i"
done

Just play a little bit around with it.

Feb 4, 2011

Some basic linux commands 1

First steps with linux shell.


Path

In linux there absolute and relative path.
An absolute path is from the root directory /. If you are in /tmp and want to do something with /home/myuser the absolute path is /home/myuser.
A relative path is the path from your current position. For example ../home/myuser


ls

With ls you can list (LiSt) the current directory. This basic tool shows you its content.
You could use the -l parameter to show the files with some informations like size or owner.
If you append the -a parameter you could see the hidden files. (Files and directories that start with a dot: .hidden)
If you want to use both parameter you could do:
ls -la or ls -l -a or ls -al or ls -a -l


pwd

pwd shows the full path of your current directory


cd(Change Directory)

With cd you could change your current directory.
cd ../ or cd /home/myuser


rm (ReMove)

With rm you could delete files. rm /path/to/file.
If you want to delete a directory you have to use the -R parameter.
rm -R /tmp/delete


mv (MoVe)

With the mv command you could move or rename a file.
mv /tmp/old /tmp/new


cat

The cat command allows you to view the content of a file.
cat /etc/passwd

Feb 3, 2011

How to run a shell script after login

You can run every shell script you like if you put it into the /etc/profile file.

After login most linux systems run the /etc/profile script. (many others too, but this one would do it)

If you want a script to run for a single user only, you can use the .profile file in users home directory
(/home/username/.profile)

If your script is called afterlogin.sh and located at /opt/afterlogin.sh you just need to add:
bash /opt/afterlogin.sh or /opt/afterlogin.sh (if execute bit is set! chmod +x /opt/afterlogin.sh)
to your /etc/profile or /home/username/.profile file.

After login your script will execute now.