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.
Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts
Sep 12, 2011
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.
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
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
How to make backup's with rsnapshot
Rsnapshot is a very easy to use backup system for linux.
There just a view steps to make it working.
In this scenerio we have a fileserver called fileserver.mydomain and a USB backup drive mounted at /opt/backupdrive
Installation
On Ubuntu or Debian just type:
apt-get update && apt-get install rsnapshot -y
Configuration
There is a main config file located at /etc/rsnapshot.conf
You need to change some lines in this file:
Around line 27 you have to change the snapshot_root to /opt/backupdrive
Around line 102 you have to set some intervals:
#interval hourly 24
interval daily 7
interval weekly 4
interval monthly 12
You could activate hourly intervals but it costs to much resources for a fileserver to make a hourly backup.
Now you can setup the backup sources. Around line 203 you can enter some line like:
backup /home/ fileserver.mydomain/
backup /etc/ fileserver.mydomain/
backup /var/www/ fileserver.mydomain/
backup /opt/stuff/ fileserver.mydomain/
These lines describe what rsnapshot has to backup.
(Example: backup /home/ directory on fileserver)
Now rsnapshot is prepared to backup some files.
Usage
You surely want rsnapshot to backup in some intervals so you don't have to do that manuel.
This is done by cronjobs. Just open /etc/crontab with the editor of your choice and add:
0 18 * * 1 root rsnapshot daily
0 18 * * 2 root rsnapshot daily
0 18 * * 3 root rsnapshot daily
0 18 * * 4 root rsnapshot daily
0 18 * * 5 root rsnapshot daily
0 18 * * 6 root rsnapshot daily
There just a view steps to make it working.
In this scenerio we have a fileserver called fileserver.mydomain and a USB backup drive mounted at /opt/backupdrive
Installation
On Ubuntu or Debian just type:
apt-get update && apt-get install rsnapshot -y
Configuration
There is a main config file located at /etc/rsnapshot.conf
You need to change some lines in this file:
Around line 27 you have to change the snapshot_root to /opt/backupdrive
Around line 102 you have to set some intervals:
#interval hourly 24
interval daily 7
interval weekly 4
interval monthly 12
You could activate hourly intervals but it costs to much resources for a fileserver to make a hourly backup.
Now you can setup the backup sources. Around line 203 you can enter some line like:
backup /home/ fileserver.mydomain/
backup /etc/ fileserver.mydomain/
backup /var/www/ fileserver.mydomain/
backup /opt/stuff/ fileserver.mydomain/
These lines describe what rsnapshot has to backup.
(Example: backup /home/ directory on fileserver)
Now rsnapshot is prepared to backup some files.
Usage
You surely want rsnapshot to backup in some intervals so you don't have to do that manuel.
This is done by cronjobs. Just open /etc/crontab with the editor of your choice and add:
0 18 * * 1 root rsnapshot daily
0 18 * * 2 root rsnapshot daily
0 18 * * 3 root rsnapshot daily
0 18 * * 4 root rsnapshot daily
0 18 * * 5 root rsnapshot daily
0 18 * * 6 root rsnapshot daily
0 20 * * 7 root rsnapshot weekly
0 22 1 * * root rsnapshot monthly
0 22 1 * * root rsnapshot monthly
Restore
If you want to restore some data you just need to look at your backupdrive.
Look for example at /opt/backupdrive/daily.0/fileserver.mydomain/.
There your files.
Solve every Windows problem
First reboot.
If that doesnt work give WUPI a try.
Its a free diskless Ubuntu Desktop installer.
You can install Ubuntu from Windows and you can have both: Windows and Linux installed as a dual boot system.
This not really solves every Windows problems but it helps to avoid some issues. ;-)
By the way a reboot solves most Windows problems.
If that doesnt work give WUPI a try.
Its a free diskless Ubuntu Desktop installer.
You can install Ubuntu from Windows and you can have both: Windows and Linux installed as a dual boot system.
This not really solves every Windows problems but it helps to avoid some issues. ;-)
By the way a reboot solves most Windows problems.
Feb 3, 2011
How to SSH login without password
If you want to connect to a remote machine via SSH and without password you have to setup a public key.
You need to generate a public and a private key.
Lets say your username is linuxuser. On your local system and your remote system.
After login you need to run:
ssh-keygen -t rsa -b 4096
You need to generate a public and a private key.
Lets say your username is linuxuser. On your local system and your remote system.
After login you need to run:
ssh-keygen -t rsa -b 4096
-t rsa is the encryption method and -b 4096 tells ssh-keygen to use 4096 bit for keys.
After that you have to ensure that your keys are located at /home/linuxuser/.ssh/. In most cases there called
id_rsa and id_rsa.pub.
Now you need to put the public key into the authorized_keys file of your target host. (lets call the host: linuxhost)
First Transfer the key: scp /home/linuxuser/.ssh/id_rsa.pub linuxuser@linuxhost:/home/linuxuser/id_rsa.pub .
Then login to the remote host (last time with password.. hopefully :-)) and enter:
cat /home/linuxuser/id_rsa.pub >> /home/linuxuser/.ssh/authorized_keys; rm /home/linuxuser/id_rsa.pub
The last step is to ensure that your private key permissions are 600. ( chmod 600 /home/linuxuser/.ssh/id_rsa )
Now you can just login to your remote machine :-)
ssh linuxhost (maybe you have to use the -i parameter: ssh -i ~/.ssh/id_rsa)
Thats it!
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.
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.
This is all about...
This Blog is about Linux, Google, Cloud Computing and SaaS!
Any questions or ideas? Leave a comment!
Subscribe to:
Posts (Atom)