Posts Tagged ‘Delete’

Remove \r (Carriage Return) from string with standard bash shell / sed / tr / vim or awk – Replace \r hidden messy characters from files

Tuesday, February 10th, 2015

remove_r_carriage_return_from_string_with-standard-bash_shell_sed_tr_or_awk_replace_annoying_hidden_messy_characters_from_files

I've been recently writting this Apache webserver / Tomcat / JBoss / Java decomissioning bash script. Part of the script includes extraction from httpd.conf of DocumentRoot variable configured for Apache host.
I was using following one liner to grep and store DocumentRoot set directory into new variable:

documentroot=$(grep -i documentroot /usr/local/apache/conf/httpd.conf | awk '{ print $2 }' |sed -e 's#"##g');

Above line greps for documentroot prints 2nd column of the matchi (which is the Apache server set docroot and then removes any " chars).

However I faced the issue that parsed string contained in $documentroot variable there was mysteriously containing r – return carriage – this is usually Carriage Return (CR) sent by Mac OS and Apple computers. For those who don't know the End of Line of files in UNIX / Linux OS-es is LF – often abreviated as n – often translated as return new line), while Windows PCs use for EOF CR + LF – known as the infamous  rn. I was running the script from the server which is running SuSE SLES 11 Linux, meaning the CR + LF end of file is standardly used, however it seem someone has editted the httpd.conf earlier with a text editor from Mac OS X (Terminal). Thus I needed a way to remove the r from CR character out of the variable, because otherwise I couldn't use it to properly exec tar to archive the documentroot set directory, cause the documentroot directory was showing unexistent.

Opening the httpd.conf in standard editor didn't show the r at the end of
"directory", e.g. I could see in the file when opened with vim

DocumentRoot "/usr/local/apache/htdocs/site/www"

However obviously the r character was there to visualize it I had to use cat command -v option (–show-nonprinting):

cat -v /usr/local/apache/conf/httpd.conf

DocumentRoot "/usr/local/apache/htdocs/site/wwwr"


1. Remove the r CR with bash

To solve that with bash, I had to use another quick bash parsing that scans through $directory and removes r, here is how:

documentroot=${documentroot%$'r'}

It is also possible to use same example to remove "broken" Windows rn Carriage Returns after file is migrated from Windows to Liunx /  FreeBSD host:

documentroot=${documentroot%$'rn'}

 

2. Remove r Carriage Return character with sed

Other way to do remove (del) Windows / Mac OS Carriage Returns in case if Migrating to UNIX is with sed (stream editor).

sed -i s/r// filename >> filename_out.txt


3. Remove r CR character with tr

There is a third way also to do it with (tr) – translate or delete characters old shool *nix command:

tr -d 'r' < file_with_carriagereturns > file_without_carriage_returns

 

4. Remove r CRs with awk (pattern scanning and processing language)

 awk 'sub("$", "r")' inputf_with_crs.txt > outputf_without_crs.txt


5. Delete r CR with VIM editor

:%s/r//g


6. Converting  file DOS / UNIX OSes with dos2unix and unix2dos command line tools

For sysadmins who don't want to bother with writting code to convert CR when moving files between Windows and UNIX hosts there are dos2unix and unix2dos installable commands.

All done Cheers ! 🙂

How to add a new MySQL user to have INSERT,UPDATE,DELETE permissions to a Database

Tuesday, October 25th, 2011

I needed to add a newly created MySQL user with no access to any database with no special permissions (user is created from phpmyadmin) with some permissions to a specific database which is used for the operation of a website, here are the MySQL CLI client commands I issued to make it work:

# mysql -u root -p
mysql> GRANT ALL ON Sql_User_DB.* TO Sql_User@localhost;
mysql> FLUSH PRIVILEGES;

Where in the Example Sql_User_DB is my example database to which the user is granted access and my sample user is Sql_User .
Note that without FLUSH PRIVILEGES; new privileges might not be active. 

To test further if all is fine login with Sql_User and try to list database tables.

$ mysql -u Sql_User -p
password:
mysql> USE Sql_User_DB;
mysql> SHOW TABLES;
...

How to Delete Windows XP temporary files from command line / Batch script to Delete Windows temp files on every system restart

Thursday, August 23rd, 2012

In case you need to DELete Windows temporary files directory to save some free space on an old PC or (group of PCs) in a raw you might prefer to use this CLI command lines:

DEL /F /S /Q %TEMP%
DEL /f /q /s "%SYSTEMDRIVE%\Documents and Settings\LocalService\Cookies\*.*"
DEL /f /q /s "%SYSTEMDRIVE%%HOMEPATH%\Cookies\*.*"
DEL /f /q /s "%SYSTEMDRIVE%\Documents and Settings\LocalService\Local Settings\Temp\*.*"
DEL /f /q /s "%SYSTEMDRIVE%\Documents and Settings\NetworkService\Local Settings\Temp\*.*"
DEL /f /q /s "%SYSTEMDRIVE%\Documents and Settings\Default User\Local Settings\Temp*.*"
DEL /f /q /s "%SYSTEMDRIVE%%HOMEPATH%\Local Settings\Temp\*.*"
DEL /f /q /s "%WINDIR%\Temp\*.*"
DEL /f /q /s "%TEMP%\*.*"
DEL /f /q /s "%SYSTEMDRIVE%\Documents and Settings\LocalService\Local Settings\Temporary Internet Files\*.*"
DEL /f /q /s "%SYSTEMDRIVE%%HOMEPATH%\Local Settings\Temporary Internet Files\*.*"
RD /q /s %TEMP%
RD /q /s %WINDIR%\Temp
RD /q /s "%SYSTEMDRIVE%\Documents and Settings\Default User\Local Settings\Temp"
RD /q /s "%SYSTEMDRIVE%\Documents and Settings\LocalService\Local Settings\Temp"
RD /q /s "%SYSTEMDRIVE%\Documents and Settings\Default User\Local Settings\Temp"
RD /q /s "%SYSTEMDRIVE%%HOMEPATH%\Local Settings\Temp"

Another helpful thing for MS Windows users is cleaning up Windows Tempory Files on every system restart (reboot); doing so is possible by setting below’s short batch script to exec on every system boot:

@ECHO OFF
IF NOT %temp% == %tmp% GOTO both_
GOTO single
:both
DEL %temp%\*.* /F /S /Q
DEL %tmp%\*.* /F /S /Q
CLS
ECHO Deleted all files in the TEMP folder: %temp%
ECHO Deleted all files in the TMP folder: %tmp%
GOTO end
:single
DEL %temp%\*.* /F /S /Q
CLS
ECHO Deleted all files in the TEMP folder: %temp%
:end

You can download the script clean_windows_temp_files_on_win_start.com here . The script is great tool for Windows administrators of Win Domain Controllers or University / educational M$ Windows based networks, where PC security is at high risk. Setting the script to run on even “non-critical” home PCs is a great idea as it can save you a lot of troubles with SpyWare, Malware Viruses and other Windows targetted “Bad-Wares” 🙂

Cheers 🙂

Error from park wrapper: mydomain.com is already configured. Sorry, that domain is already setup (remove it from httpd.conf) – How to solve

Monday, July 4th, 2011

If you’re administrating a Cpanel server and you come across an error message while trying to use cpanel’s domain addon menu and you want to fix that you will need to do the following logged in as root over an ssh connection:

1. Remove dns related stuff in /var/named and /var/named/cache cpanel:~# rm -f /var/named/mydomain.com.dbcpanel:~# rm -f /var/named/cache/mydomain.com.db

2. Edit the current used httpd.conf on the server and remove all virtualhost domain definitions

cpanel:~# vim /etc/httpd/conf/httpd.conf
# find the mydomain.com Virtualhost definitions and completely remove them

3. Remove any domain occurance in /var/cpanel/users

cpanel:~# cd /var/cpanel/users/
cpanel:/var/cpanel/users# grep -rli 'mydomain.com' *
/var/cpanel/users/hipo
cpanel:~# vim /var/cpanel/users/hipo
# remove in above file any domain related entries

3. Remove anything related to mydomain.com in /etc/userdomains and /etc/localdomains

cpanel:~# vim /etc/userdomains
cpanel:~# vim /etc/localdomains
# again look inside the two files and remove the occuring entries

4. Edit /etc/named.conf and remove any definitions of mydomain.com

cpanel:~# vim /etc/named.conf
# in above file remove DNS configuration for mydomain.com

5. Run /scripts/updateuserdomains

cpanel:~# /scripts/updateuserdomains

6. Delete any valias configurations

cpanel:~# rm -f /etc/valiases/mydomain.com
cpanel:~# rm -f /etc/vdomainaliases/mydomain.com
cpanel:~# rm -f/etc/vfilters/mydomain.com

7. Remove any occurance of mydomain.com in the user directory which experiences the Error from park wrapper: error

Let’s say the user testuser is experiencing the error, in that case you will have to remove:

cpanel:~# rm -rf /home/testuser/public_html/mydomain.com

8. Restart Cpanel

This step is optional though I think it’s also a good practice as it will at least restart the Cpanel webserver (Apache or Litespeed depending on your conf)

cpanel:~# /etc/init.d/cpanel restart

Now try to add up the domain via the Cpanel domain addon interface, hopefully the issue should be fixed by now. If not you might also check if there is no some record about mydomain.com in the mysql server.
Cheers 😉

Howto delete multiple files in Linux and FreeBSD / How to deal with “Argument list too long” error while deleting many files in directory

Wednesday, April 7th, 2010

Linux has some Limitations on the number of files you can delete within a directory, therefore if you try to delete let’s say 100000 files with a quarantine mails from spamassassin.
In that case you are about to face an error Argument list too long . The amount of files you can delete in Linux is tied with something specified by a file:
/usr/include/linux/limits.h
This limitation is a limitation caused by kernel_limits. In order to check the limitation on your Linux distribution, you have to execute the command:

egrep ARG_MAX /usr/include/linux/limits.h

You should receive a result on most Linux distrubutions similar to:
#define ARG_MAX 131072 /* # bytes of args + environ for exec() */

The 131072 is actually a default limitation on Debian GNU/Linux as well.The reason for the error is that the the maximum number (in bytes) of the arguments to a command could be equal max to the ARG_MAX defined in the limits.h.
For instance rm -f * in a directory with 40000 fileswould be evaluted as rm -f file1 file2 file3 … file40000. Therefore at a certain point the maximum limitation of 131072 bytes long for arguments or 128KB is about to be reached and then the command let’s say ls * would refuse to list the files in the directory showing up the annoying Argument list too long error.
There are a couple of ways to deal with that unpleasant situation.

1. You can use the linux find command to delete the files, you have to execute after changing dir (cd) to the directory where the multiple files are located:
find . -exec rm -fr {} ; 2. Second approach to the problem is passing the xargs command to find .
For instance execute the command:

find . -name "*" -print | xargs rm

3. In FreeBSD to get around the “Argument list too long” problem”, in bash shell you have to execute:

for files in *.*; do rm -f $files; done

4. Another possible way is to increase the ARG_MAX value in limits.h though this approach in my personal belief could have a negative impact on some productive servers, therefore it’s not a recommended.
Yet if you desire to do so simply edit /usr/include/linux/limits.h and change the ARG_MAX to your value of choice.

Howto delete empty directories in GNU /Linux with find linux command

Wednesday, April 7th, 2010

Ever wondered how you can delete all the empty directories in Linux?
I bet you did, there are many ways to achieve that in GNU/Linux, however here is one way you might go:
First it is probably a good idea to list the empty directories and examinethe empty directories before you take the next step and execute a command to delete them:

find . -depth -type d -empty

Now after you take a close look in the directories, next step to partake is delete the directories.

find . -depth -type d empty -exec rmdir {} ;

Be aware that in the above examples, the first one would list all directories in your current directory in which you
execute the command, the second example will delete all the empty directories starting from your current directory unto thedeepest located empty directory in the directory tree.