There are times that we need to import some python module and, for whatever reasons, we will not know the name of the module until run-time. This can be achieved by built-in function __import__().
Dynamic loading python module is useful, for example, if we plan to read configuration from a file. Here is an example that allows us to load the module specified in sys.argv[1]:
config_file = sys.argv[1]
config = __import__(config_file)
For newer versions (2.7 and up), there are convenience wrappers for __import__(). We may use module importlib:
import importlib
config_file = sys.argv[1]
config = importlib.import_module(config_file)
Both __import__() and importlib.import_module() search modules in certain locations (e.g., sys.modules, sys.meta_path, sys.path). There are times that modules being loaded are not in those default locations. This can be achieved by using imp.load_source(), which allows us to load and initialize modules implemented as Python source files:
import imp
pathname = sys.argv[1]
config = imp.load_source(name, pathname)
where pathname is the path name of the configuration file.
A journal on information technology: things I studied, worked, thought, but can't stay in my memory.
Sunday, July 7, 2013
Thursday, July 4, 2013
A quick and simple way to wipe a hard drive
For various reasons, it is necessary to wipe hard drives before disposing them. Writing disk with all zeros should suffice most occasions. This can be done as:
dd if=/dev/zero of=/dev/sdXY
where /dev/sdXY is the device name of the hard drive to work on. To get a better performance, we may need to set bs option, e.g.,
dd if=/dev/zero of=/dev/sdb1 bs=10M
to make dd read and write in 10M bytes at a time.
dd if=/dev/zero of=/dev/sdXY
where /dev/sdXY is the device name of the hard drive to work on. To get a better performance, we may need to set bs option, e.g.,
dd if=/dev/zero of=/dev/sdb1 bs=10M
to make dd read and write in 10M bytes at a time.
Tuesday, July 2, 2013
Some Usages of cpio
The command cpio allows us to copy files between archives and directories. There are three operation modes: output (-o), input (-i), and pass-through (-p).
To create an archive for directory tree src, we issue:
find src -print0 | cpio -ov0 > src.cpio
To extract files from its archive just created, we issue:
cpio -ivd < src.cpio
To copy files from directory tree src to directory des, we issue:
find src -print0 | cpio -pmd0 dest
To copy files from src to dest that are less than 2 days old and whose names contain 'txt', we issue:
find src -mtime -2 -print0 | grep txt | cpio -pmd0 dest
To copy files from src to dest that are less than 2 days old and which contain the word 'txt', we issue:
find src -mtime -2 | xargs grep -l txt | cpio -pmd0 dest
Combining with ssh (or, natcat if you are not concerned with security), cpio allows us to access remote hosts. Here are some example:
To backup the directory tree src on a remote host, we may issue:
find src -print0 | cpio -oaV0 -H tar -O user@remotehost:src.tar
which uses ssh to copy archive file from localhost to remote host.
To copy files from src to a remote host, we issue:
find src -print0 | cpio -oaV0 -H tar | ssh user@remotehost "cpio -imd"
To create an archive for directory tree src, we issue:
find src -print0 | cpio -ov0 > src.cpio
To extract files from its archive just created, we issue:
cpio -ivd < src.cpio
To copy files from directory tree src to directory des, we issue:
find src -print0 | cpio -pmd0 dest
To copy files from src to dest that are less than 2 days old and whose names contain 'txt', we issue:
find src -mtime -2 -print0 | grep txt | cpio -pmd0 dest
To copy files from src to dest that are less than 2 days old and which contain the word 'txt', we issue:
find src -mtime -2 | xargs grep -l txt | cpio -pmd0 dest
Combining with ssh (or, natcat if you are not concerned with security), cpio allows us to access remote hosts. Here are some example:
To backup the directory tree src on a remote host, we may issue:
find src -print0 | cpio -oaV0 -H tar -O user@remotehost:src.tar
which uses ssh to copy archive file from localhost to remote host.
To copy files from src to a remote host, we issue:
find src -print0 | cpio -oaV0 -H tar | ssh user@remotehost "cpio -imd"
Monday, July 1, 2013
Access UFS File System under Linux
Unix file system (UFS) is widely used in many Unix systems, for example, FreeBSD, OpenBSD, and HP-UX. There are times that we need to access UFS under Linux systems. The following command allows us to mount UFS2 for read-only (ro) under Linux systems:
mount -t ufs -o ufstype=ufs2,ro /dev/sdXY /mnt/path
Write support for UFS is not compiled into Linux kernels by default. One needs to properly configure and compile kernels for write support.
mount -t ufs -o ufstype=ufs2,ro /dev/sdXY /mnt/path
Write support for UFS is not compiled into Linux kernels by default. One needs to properly configure and compile kernels for write support.
Saturday, June 29, 2013
Netcat for File Transfer
File transfer is one of the most practical usages of netcat.
To transfer a file named filename from client to server, we first issue the following command on server:
nc -l -p 1234 > filename
where 1234 is the port number used by the server. On the client side, we issue:
nc -q 10 server 1234 < filename
where '-q 10' specifies to wait 10 seconds and then quit after EOF on stdin. This would cause the server to quit.
To transfer a director tree named /path from client to server, we issue the following command on server:
nc -l -p 1234 | tar xvzf -
On the client side, we issue:
tar cvzf - /path | nc -q 10 server 1234
If we want to reverse the direction of file transfer, i.e., client pulls file from server, we use:
nc -q 10 -l -p 1234 < filename
on server, and
nc server 1234 > filename
on client. Similarly, to reverse the direction of directory tree transfer, we use:
tar cvzf - /path | nc -q 10 -l -p 1234
on server, and
nc server 1234 | tar xvzf -
on client.
To transfer a file named filename from client to server, we first issue the following command on server:
nc -l -p 1234 > filename
where 1234 is the port number used by the server. On the client side, we issue:
nc -q 10 server 1234 < filename
where '-q 10' specifies to wait 10 seconds and then quit after EOF on stdin. This would cause the server to quit.
To transfer a director tree named /path from client to server, we issue the following command on server:
nc -l -p 1234 | tar xvzf -
On the client side, we issue:
tar cvzf - /path | nc -q 10 server 1234
If we want to reverse the direction of file transfer, i.e., client pulls file from server, we use:
nc -q 10 -l -p 1234 < filename
on server, and
nc server 1234 > filename
on client. Similarly, to reverse the direction of directory tree transfer, we use:
tar cvzf - /path | nc -q 10 -l -p 1234
on server, and
nc server 1234 | tar xvzf -
on client.
Thursday, June 27, 2013
Using findutils to delete files
Here is a summary from the deleting files page.
The most efficient and secure method to delete any file with name ending in '~' in the directory /path is:
find /path -name \*~ -delete
Using command xargs may allow us to achieve same efficiency but is not as secure:
find /path -name \*~ -print0 | xargs -0 /bin/rm
where '-print0' specifies using ASCII NUL to separate the entries in the file list, and similarly '-0' for command xargs.
If the '-delete' action is not available, we may use action '-execdir' or '-exec':
find /path -name \*~ -execdir /bin/rm {} \+
find /path -name \*~ -exec /bin/rm {} \+
Action '-execdir' is secure but less portable. On the other hand, action '-exec' is most efficient portable but insecure. These two actions can be used for doing things other than deleting files.
The most efficient and secure method to delete any file with name ending in '~' in the directory /path is:
find /path -name \*~ -delete
Using command xargs may allow us to achieve same efficiency but is not as secure:
find /path -name \*~ -print0 | xargs -0 /bin/rm
where '-print0' specifies using ASCII NUL to separate the entries in the file list, and similarly '-0' for command xargs.
If the '-delete' action is not available, we may use action '-execdir' or '-exec':
find /path -name \*~ -execdir /bin/rm {} \+
find /path -name \*~ -exec /bin/rm {} \+
Action '-execdir' is secure but less portable. On the other hand, action '-exec' is most efficient portable but insecure. These two actions can be used for doing things other than deleting files.
Tuesday, June 25, 2013
Use command find to clean up your file system
Some editors create back-up files with the the same file name and a '~' suffix. Once you are done with editing, there is no need to keep these back-up files. The command find allows us to find and remove all such back-up files in working directory:
find . -name \*~ -exec rm {} \;
If you want to remove files not been accessed for more than one year (365 days), you may issue:
find . -atime +365 -exec rm {} \;
Command find comes with many other options, for example, you may use: 'mtime' for last modified, 'ctime' for last change, or even 'inum' for inode number.
find . -name \*~ -exec rm {} \;
If you want to remove files not been accessed for more than one year (365 days), you may issue:
find . -atime +365 -exec rm {} \;
Command find comes with many other options, for example, you may use: 'mtime' for last modified, 'ctime' for last change, or even 'inum' for inode number.
Subscribe to:
Posts (Atom)