Hot answers tagged linux
64
Use truncate:
truncate -s 14M filename
From man truncate:
DESCRIPTION
Shrink or extend the size of each FILE to the specified size
[...]
-s, --size=SIZE
set or adjust the file size by SIZE
Note: truncate may not be available on your system, e.g. on Mac OS X it's not installed by default (but you can easily install it, using macports for ...
47
Call the command like so:
mkdir -- -a
The -- means that the options end after that, so the -a gets interpreted literally and not as an option to mkdir. You will find this syntax not only in mkdir, but any POSIX-compliant utility except for echo and test. From the specification:
The argument -- should be accepted as a delimiter indicating the end of ...
35
The following commands create a 14MB file called foo:
Create a file filled with random data
dd if=/dev/urandom of=foo bs=14MB count=1
...
19
The simplest way that should work with any reasonable program is to use a relative path name in front of the -, e.g. mkdir ./-a will create a directory called -a in the current working directory.
The most common example of using this "trick" is when you want to remove a file which begins with a dash, so you can do rm ./-a.
12
dd bs=1MB count=14 if=/dev/zero of=<yourfilename>
WARNING dd will silently overwrite anything you have the rights to write to, if you specify it either willingly or by accident. Make sure you understand dd, eg. with reading dd --help, and you don't screw up your dd command lines or else you can easily create irreversible data loss.
For start, if= is ...
9
You can use touch. E.g::
touch -d '2007-01-31 8:46:26' file
Or often easier, if you have a file2 which has already the mtime, you can copy the time with -r:
touch -r file2 file
There is also the -t option with its strange format:
touch -t [[CC]YY]MMDDhhmm[.ss] file
9
xargs does all the magic:
find . -name test -type d -print0|xargs -0 rm -r --
xargs executes the command passed as parameters, with the arguments passed to stdin.
This is using rm -r to delete the directory and all its children.
The -- denotes the end of the arguments, to avoid a path starting with - from being treated as an argument.
-print0 tells ...
9
grep uses regular expressions by default, any pattern given to grep is assumed to be a regular expression unless you use the -F switch.
Expressions such as *? are not part of the POSIX regex language which is what grep uses by default. You can use such constructs by specifying a different regex language, Perl for example:
$ cat a.txt
aaaaaaaa
...
8
Two things:
You should always check return values from system and library functions. If you had
if(remove(dirp->d_name)<0)
perror(dirp->d_name);
else
files_deleted++;
then you'd see what was happening.
The reason your code doesn't work is because remove() and unlink() require the full path to the file in question, whereas readdir() ...
7
find /path/to/dir -name "test" -type d -delete
-name: looks for the name passed. You can use -regex for providing names based on regular expressions
-type: looks for file types. d only looks for directories
-delete: action which deletes the list found.
Alternatively:
find /path/to/dir -name "test" -type d -exec rm -rf {} \;
As J.F. Sebastian stated ...
7
POSIX defines that...
...if cd was not given any arguments, it will use the value of $HOME if the variable is set, and do nothing otherwise (although it also says that results are unspecified). — cd
...if an argument is ~ or begins with ~/, the tilde will be expanded to the value of $HOME if the variable is set; the results are unspecified otherwise. — ...
7
How easy it would be to break this setup (to decrypt the root
filesystem and make it boot from any sd card)?
How hard it is to "break" your setup depends on the number of bits of entropy in whatever method you're using to sign/encrypt the filesystem itself (as this determines the total number of unique combinations that can be used to brute-force the ...
6
Technical answer: traditionally, egrep used a deterministic finite automaton (DFA) internally while grep used a non-deterministic finite automaton (NFA). These days, GNU grep and egrep take a hybrid NFA/DFA approach.
According to Friedl's book Mastering Regular Expressions, to discover if your egrep (for example) has an NFA engine or if it has a DFA engine ...
6
You are quoting one of the $1 's but not the other. If your path contains spaces, quoting is essential. To be safe, it's best to assume that every path a script receives contains problematic characters, and quote every path.
Here's my suggestion:
gs -q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.3 \
...
6
First of all, stop executing everything as root. You never really need to do this. Only run individual commands with sudo if you need to. If a normal command doesn't work without sudo, just call sudo !! to execute it again.
If you're paranoid about rm, mv and other operations while running as root, you can add the following aliases to your shell's ...
6
You have the arguments swapped. Try this:
zip -d gallery.zip "picture_43_9.jpg"
From the zip(1) man page:
-d
--delete
Remove (delete) entries from a zip archive. For example:
zip -d foo foo/tom/junk foo/harry/* *.o
will remove the entry foo/tom/junk, all of the files that start with
foo/harry/, and all of the files that end with .o (in any ...
6
MemTest86+ can run deep level checks on your RAM sticks. A Linux flavored version of Prime95 could also be used to stress test your RAM modules.
http://memtest86.com/
http://mersenne.org/freesoft/
Prime95 won't give you specifics on if the RAM is bad, typically can just cause your computer to crash, which can be indicative of a RAM failure. MemTest86+ ...
6
This is because jobs shows background commands started from (belonging to) the same shell. The shell processes running under your desktop terminal and under ssh terminal are different.
See http://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html
To be able to control your processes as jobs from different terminals you can use screen or ...
5
A third option would be
cat 'file name with space'
where the file name may contain everything but the '.
If it does, such as file n'ame, replace every ' with '\'':
cat 'file n'\''ame'
5
You could simply use find like this
find some_folder ! -name dontshoot.txt -delete
This will delete all files and folders recursively except the dontshoot.txt files.
As noted in the other comments it's always helpful to test it without deleting, which can simply be done like this.
find some_folder ! -name dontshoot.txt
However this will list all ...
5
Line wrap disabling support is terminal dependent. For example if using screen you can hit Ctrl-A Ctrl-R to toggle line wrap.
Otherwise, you might try setterm -linewrap off with or without increasing the number of terminal columns with stty (haven't tried this).
Terminal emulators like PuTTY (if you connect to the server from a Windows box) have their own ...
5
The solution is to spin down the drive via software before turning it off and unplugging it. The best way to do this is with a utility called scsiadd. This program can add and remove drives to Linux’s SCSI subsystem. Additionally, with fairly modern kernels, removing a device will issue a stop command, which is exactly what we’re looking for.
Run:
$ sudo ...
5
Cronjobs run as whatever user set the crontab up. They don't run as a special user. Usually, if you know a crontab exists, you know who set it up. You can see the crontabs of the current user by running
crontab -l
You can find user-specific crontabs in the directory /var/spool/cron/crontabs/. Each user who has created a crontab will have a file (whose ...
5
Quicky script, adapt as you see fit:
#!/usr/bin/env bash
find /project/ -name '*.pdf' -print0 | while read -d $'\0' i; do
if [ ! -e "${i/%.pdf/-project.zip}" ]; then
echo "${i/%.pdf/-project.zip} doesn't exist!"
fi
done
exit 0
-d $'\0' sets the delimiter for read to nullbyte, while -print0 is the equivalent for find, so this should be ...
5
mktemp, by default, creates a temporary directory under /tmp, which by default is writable by anyone; if you pass the --tmpdir option to mktemp (or set the TMPDIR environment variable), it will attempt to create a temporary directory in the directory given by the option's argument, and that will fail unless you have write permission in the given directory. ...
5
In addition to David Custer's excellent answer, I advise you to do an Internet search for the terms "Linux cheat sheet" and "Bash cheat sheet". A cheat sheet is the best mechanism for learning and recalling material that has a very large number of details. Think of it as a language phrase book.
Your Internet search will turn up many useful cheat sheets that ...
5
You need no variables. You can use diff to compare the outputs of two different commands:
s3cmd ls --list-md5 \
| tr -s ' ' \
| cut -d ' ' -f 5,6 \
| sed 's= .*/= =' \
| diff -w - <(md5sum *.gz)
You might need to sort the outputs to have the files in the same order.
4
From the Coreutils info pages for head (accessible through info coreutils 'head invocation'):
For compatibility head also supports an obsolete option syntax
-COUNTOPTIONS, which is recognized only if it is specified first.
COUNT is a decimal number optionally followed by a size letter (b,
k, m) as in -c, or l to mean count by lines, or other option
...
4
The "scalability" of the OS itself, combined with the accessible nature of open source software. It is easy to start with just the Linux kernel built with only a small subset of its drivers and then add software around it until it fits the niche required; doing this with Windows or OS X is much more work, and even then the appropriate licensing will leave ...
4
You could try mounting the external disk as a VM hard disk.
VBoxManage internalcommands createrawvmdk -filename path\to\virtualdisk.vmdk -rawdisk \\.\PhysicalDriveX
path\to\virtualdisk.vmdk is the file that will be created after you run the command. You can then mount this a virtual drive on the guest OS under the Storage settings for the guest.
...
Only top voted, non community-wiki answers of a minimum length are eligible
