Posts Tagged ‘Shell’

Here is a short script that helped me to copy files that have ben modified within the last 60 minutes to a new directory. The script uses a combination of the find and cp commands, it has to be run inside the directory that contains the new files.  In this example, I will copy all files that have been modified within the last 60 minutes from /var/www to the directory /var/newfiles, you can replace the timespan or target directory in the script to match your reqzúirements.

First, enter the /var/www directory which contains the files:

cd /var/www

Then execute this command to find the latest files and copy them to /var/newfiles:

find ./ -type f -mmin -60  -exec cp -pf {} /var/newfiles/ \;

 

 

Look here to learn how to create Visual Basic Scripts.

Using Visual Basic Script you can also access elementary Windows services, some them being really useful to irritate your friends. If at any point of the script there is an error stating that the double-quotes used in it are invalid characters, just delete the copied ones and type them again manually.
With the use of a shell, you can make VBScripts run programs and other executables. To do so you have to specify following line in the beginning of the script:

Set objShell = wscript.CreateObject(“wScript.Shell”)

Afterwards you can run commands with

objShell.run program

where you replace program with the program you want to run.
To add an executable to the autostart list via registry, you use the following script:

objShell.Regwrite “HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\some_name.exe”,”C:\the\files\path.vbs

Replace the name with any name, it really can be any name, and the path of the file with the correct path. The script will then be executed on every start-up (be careful if you use this in combination with more dangerous scripts).
To make the script itself type as if it was the keyboard, use this script (a shell must be set for this as in the first script shown here):

objShell.sendkeys”key_goes_here”

Replace key_goes_here with the keys the script is supposed to hit. Some of them are embraced by curly brackets, as for example “{CAPSLOCK}”, “{NUMLOCK}”, “{SCROLLOCK}” and “{bs}” (backspace). Most however go without, as do all letter keys and enter (“~”). Combining all the locks with a loop usually gives a great script!
To make Windows’ integrated voice say some words defined by you, use the following script:

Set objVoice = CreateObject(“SAPI.SpVoice”)
objVoice.Speak “blabla”

Replace blabla with some serious things your computer has to say to you.

If you have a directory with many files (a few thousand in my case) and need to know which one is the newest, then the following command might be useful. It returns the name and details of the newest file in a directory where it is executed:

ls -tl | sed -n 2p

Disable quota for a Linux user or group on the shell

Thursday, May 5, 2011 posted by Till

Linux user quotas can be edited with the commands edquota or setquota on the shell. While edquota opens the quota settings in a editor like vim, setquota allows you to specify the quota settings on the commandline.

Example for disabling the quota for the user “testuser”:

setquota -u testuser 0 0 0 0 -a

Example for disabling quota for the group “testgroup”:

setquota -g testgroup 0 0 0 0 -a

Rerun the last command with sudo on the Linux shell

Friday, February 19, 2010 posted by Till

Have you ever executed a command on the shell and noticed the that you had to run it with sudo? Instead of typing the command again with “sudo” in front of it, just run:

sudo !!

which will execute the command that was run before with sudo. Thanks to Planetfox for this tipp.

How to prevent a Linux system user from loggin into the system

Friday, October 23, 2009 posted by Till

If a linux system user is able to login on the shell or with SSH depends on its shell setting in /etc/passwd. If you want to prevent that a certain user is able to login, then set the shell either to /bin/false or /sbin/nologin.

Example for Debian and Ubuntu Linux for the user with the username “otheruser”:

usermod -s /bin/false otheruser

For Redhat, Fedora or CentOS use /sbin/nologin:

usermod -s /sbin/nologin otheruser

Warning: Do not set the shell for the root user to /bin/false or /sbin/nologin!

If the root user stores a file in the home directory of another user or any other directory that is owned by another user, this other user is able to delete the file even if the file is owned by root and has 700 permissions.

Example:

root@workstation:/home/otheruser# ls -la
total 8
drwxr-xr-x 2 otheruser otheruser 4096 Oct 23 11:52 .
drwxr-xr-x 3 root      root      4096 Oct 23 11:51 ..
-rwx—— 1 root      root         0 Oct 23 11:52 root_users_file

If I su now to “otheruser”, I’am able to delete the file as “otheruser” is the owner of the directory where “root_users_file” is stored:

root@workstation:/home/otheruser# su otheruser
sh-3.2$ rm root_users_file
rm: remove write-protected regular empty file `root_users_file’? y
sh-3.2$

Now to protect the file from beeing deleted, use the command chattr +i:

chattr +i root_users_file

and then try again to delete the file as “otheruser”, the action will be denied:

root@workstation:/home/otheruser# su otheruser
sh-3.2$ rm root_users_file
rm: remove write-protected regular empty file `root_users_file’? y
rm: cannot remove `root_users_file’: Operation not permitted
sh-3.2$

Now even root is not able to delete or edit the file anymore. With the command chattr -i the protection can be removed:

chattr -i root_users_file

How to convert filenames or text to lowercase on the shell

Wednesday, September 2, 2009 posted by admin

There is no simple tolower command on the bash, but with a little shell script you can convert uppercase characters to lowercase. The script uses the tr command internally for converting the chars.

Create a shell script with the name tolower:

vi /usr/local/bin/tolower

and enter the following content:

#!/bin/sh
echo $1 | tr ‘[:upper:]‘ ‘[:lower:]‘

Then make the script executable:

chmod +x /usr/local/bin/tolower

An test it by executing this command on the shell:

tolower “Thats a Test”

will convert the string to lowercase and show the result on the shell:

thats a test