Posts Tagged shell

Remove files with odd name

Sometimes an user create a file or directory with a odd filename, for example:
server@root:/root # touch /tmp/test/^Easasasasa.txt

This “^E” is the result of a CTRL+E. With “ls”, we have the following:
server@root:/tmp/test # ls -ltr
total 0
-rw-r----- 1 root sys 0 Nov 30 15:34 asasasasa.txt

Note that you cannot see the ^E in filename, so the system will not recognize it as “asasasasa.txt”:
server@root:/tmp/test # rm asasasasa.txt
rm: asasasasa.txt non-existent

The solution is work with the file’s inode:
server@root:/tmp/test # ls -lia
total 96
11 -rw-r----- 1 root sys 0 Nov 30 15:34 asasasasa.txt
5 drwxr-x--- 2 root sys 96 Nov 30 15:34 .
2 drwxrwxrwt 8 root root 49152 Nov 30 15:33 ..

The number “11” is the inode of the odd file. Now we can remove it using the find command:
server@root:/tmp/test # find . -inum 11
./asasasasa.txt
server@root:/tmp/test # find . -inum 11 -exec rm -rf {} \;

, , ,

No Comments

Convert a text to/from UPPER to/from lower

You can use the following command do convert a file content from upper case do lower case:

cat /tmp/upper_file.txt |tr '[:upper:]' '[:lower:]' > /tmp/lower_file.txt

Or the following command to convert from lower to upper case:

cat /tmp/lower_file.txt |tr '[:lower:]' '[:upper:]' > /tmp/upper_file.txt

, , ,

No Comments

How to remove/move/copy/rename file that has a non-printable name

How to remove/move/copy/rename file that has a non-printable name, for example:

backup@root:/home/userhome/test # ls -la
total 2
-rw-rw-rw- 1 root sys 0 Sep 23 15:48
drwxrwxrwx 2 root sys 96 Sep 23 15:50 .
drwxr-xr-x 5 userhome adm 1024 Sep 23 16:20 ..
-rw-rw-rw- 1 root sys 0 Sep 23 15:47 -test

The easiest one is the file named "-test", you can do a "mv -- -test xx" to rename it to test, getting rid of the "-".

The most annoying is the other file, which apparently has the name "blank", but not quite.

There are two ways to find the real file name.,"ls -lq" e "ls -lb":

backup@root:/home/userhome/test # ls -lq
total 0
-rw-rw-rw- 1 root sys 0 Sep 23 15:48 ?
-rw-rw-rw- 1 root sys 0 Sep 23 15:47 xx
backup@root:/home/userhome/test # ls -lb
total 0
-rw-rw-rw- 1 root sys 0 Sep 23 15:48 \002
-rw-rw-rw- 1 root sys 0 Sep 23 15:47 xx
backup@root:/home/userhome/test # ls -li
total 0
17334 -rw-rw-rw- 1 root sys 0 Sep 23 15:48
17332 -rw-rw-rw- 1 root sys 0 Sep 23 15:47 xx

The "ls -lb" shows the non-printable characters with "\ XXX" where XXX is the ASCII representation of the non-printable character.
The "ls -lq" shows the non-printable characters with "???" (quantity of ? is defined by the amount of non-printable caracters).
The "ls -li" shows the file inode.

To handle this file, the best option is to use the "find".

- Using the find based on the inode:
find . -inum 17334 -exec mv {} asd \;

- Using find with "wildcards" (based on the ls-lq returned):
find . -type f -name "?" -exec mv {} asd \;

Be careful when using the find with "meta character"! If a file whose name has only one character, it will enter the result.
 

, ,

No Comments

Merge many depots

When you have many depot files, you can create a single big depot and install it instead of install one by one. Let’s assume that you need install the following depots, and they are all located in the directory /tmp/install/depots:

autoconf-2.64-hppa-11.11.depot bison-2.4.1-hppa-11.11.depot gawk-3.1.7-hppa-11.11.depot sed-4.2.1-hppa-11.11.depot tcltk-8.5.7-hppa-11.11.depot texinfo-4.13-hppa-11.11.depot zip-3.0-hppa-11.11.depot

To create a single depot, run the follow commands:
# cd /tmp/install/ for i in `ls depots/*.depot` ;do swcopy -s /tmp/install/$i \* @/tmp/install/big;done

Now, to install it, you can use:
# swinstall -s /tmp/install/big

 

, , ,

No Comments

rlogin access denied

In some specific situations, you need use rlogin to remote access a server, but you can face the following error:

clientServer:~ # rlogin rloginServer
Password:
Password:
Login incorrect

login: root
Password:
Login incorrect

login: root
Password:
Login incorrect

login: root
Password:
Login incorrect

rlogin: connection closed.

If you check the /var/log/secure log on the “rloginServer”, you will find the following messages:

Jun 15 10:44:41 rloginServer rlogind[16640]: pam_securetty(rlogin:auth): access denied: tty 'rlogin' is not secure !
Jun 15 10:44:41 rloginServer rlogind[16640]: pam_rhosts_auth(rlogin:auth): denied to root@10.11.4.9 as root: access not allowed
Jun 15 10:44:47 rloginServer login: pam_securetty(remote:auth): access denied: tty 'pts/0' is not secure !
Jun 15 10:44:51 rloginServer login: FAILED LOGIN 1 FROM 10.11.4.9 FOR root, Authentication failure
Jun 15 10:44:53 rloginServer login: pam_securetty(remote:auth): access denied: tty 'pts/0' is not secure !
Jun 15 10:44:58 rloginServer login: FAILED LOGIN 2 FROM 10.11.4.9 FOR root, Authentication failure
Jun 15 10:44:58 rloginServer login: pam_unix(remote:auth): bad username []
Jun 15 10:44:58 rloginServer login: pam_succeed_if(remote:auth): error retrieving information about user
Jun 15 10:44:58 rloginServer login: FAILED LOGIN 3 FROM 10.11.4.9 FOR , User not known to the underlying authentication module
Jun 15 10:44:59 rloginServer login: pam_unix(remote:auth): bad username []
Jun 15 10:44:59 rloginServer login: pam_succeed_if(remote:auth): error retrieving information about user
Jun 15 10:44:59 rloginServer login: FAILED LOGIN SESSION FROM 10.11.4.9 FOR , User not known to the underlying authentication module

The problem here, is that “rlogin” is not a “secure” shell. To configure it, you should add “rlogin” (without the quotes) to /etc/securetty .

After that, you will be able to access the rlogin server.

, ,

No Comments

Setting session timeout on Linux

To set an automatically shell timeout on Linux (that Will logoff that session after X seconds), you just need setup the following variable (put it in your /etc/profile):

# echo “TMOUT=300; readonly TMOUT; export TMOUT” >> /etc/profile

The “readonly” option will not allow an normal user change (ou unset) it.

,

No Comments

Rotate log files in Linux

An easy way (shell script) to rotate log files in Linux can be:

#!/bin/bash
# Include the following line in crontab:
#00 5 * * * /bin/rotate_logs.sh > /dev/null 2>&1
#

DATE=`date "+%Y%m%d"`
cd /var/log/

for i in messages secure cron lastlog
do
cp ${i} ${i}.${DATE}
> ${i}
gzip -9 ${i}.${DATE}
done

/etc/init.d/syslog restart

If you have any other log to rotate, you can change the line 8 and 10.

,

No Comments

Disable CTRL+C on Linux

To disable CTRL+C on Linux, you can use the following command:

# stty intr undef

 

,

No Comments