Tag Info

Hot answers tagged

41

cd <the directory you want> find . -type f ! -iname "*.pdf" -delete The first command will take you to the directory in which you want to delete your files The second command will delete all files except with those ending with .pdf in filename For example, if there is a directory called temp in your home folder: cd ~/temp then delete files: ...


24

With bash's extended shell globbing, you could remove any files with extensions other than .pdf using rm -- *.!(pdf) As noted by @pts, the -- characters indicate the end of any command options, make the command safe in the rare case of files whose names start with a - character. If you want to delete files without any extension as well as those with ...


11

The easiest approach: Create another directory somewhere (if you're only deleting in one directory, not recursively, it can even be a subdirectory); move all the .pdf's there; delete everything else; move the pdf's back; delete the intermediate directory. Quick, easy, you can see exactly what you're doing. Just make sure the intermediate directory is on ...


11

Delete to trash: $ cd <the directory you want> $ gvfs-trash !(*.pdf) Or via mv command: mv !(*.pdf) ~/.local/share/Trash/files


6

I would have done this like this: #!/bin/bash if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1 fi Mind that id=0 is what is used inside the kernel to identify the superuser (or root). edit: it will be even better to redirect to STDERR. Here is a one-liner :) (( EUID )) && echo ‘You need to be ...


4

save this in a file and then run bash <filename> #!/bin/bash ifconfig eth0 | grep "inet addr" being more accurate to get only number showing IP address: #!/bin/bash ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1


4

When you type sh somescript.sh, the system looks for sh in the path not somescript.sh. Then the system finds sh and passes the argument somescript.sh to it. Then sh looks for somescript.sh in the current folder or in the specified folder ~/bin/ if you typed sh ~/bin/somescirpt.sh. To make the system look for somescript.sh in the path use it directly ...


4

The exact same command should work fine in a script: #!/usr/bin/env bash find te*/my\ files/ -print If you need to have it as a variable, it gets a bit more complex: #!/usr/bin/env bash search='te*/my\ files/' eval find "$search" -print WARNING: Using eval like that is not safe and can result in executing arbitrary and possibly harmful code if your ...


3

Seems like you want something like this, $ cat file foo bar $ sed 's/\([a-z]\+\)/\1 blah/g' file foo blah bar blah \([a-z]\+\) captures one or more lowercase letters. Then the matched characters are replaced by the characters which are present inside the group index 1 plus the string " blah".


3

Just like the error says: createdep is not a program. Change this: echo $(createdep) mkdir createdep to this: echo "$createdep" mkdir "$createdep" Note that the format string for date can contain regular characters too, so you don't need the "now" variable: createdep=$(date +"nowt_%H_%M_%S")


3

dd if=/dev/random of=~/student/john.bin bs=1024 count=12000 or dd if=/dev/urandom of~/student/john.bin bs=1024 count=12000 Where bs is the size of each random input you want (1024 = 1Kb), and count is how many of those blocks you want. ie. bs=1024, count=12000: 12MB bs=512, count=24000: 12MB bs=256, count=48000: 12MB etc.


3

I usually solve such problems from the interactive Python interpreter: mic@mic ~ $ python >>> import os >>> for f in os.listdir('.'): ... if not f.endswith('.pdf'): ... os.remove(f) It might be longer than a one-liner with find or xargs, but it's extremely resilient, and I know exactly what it does, without having to research it ...


3

The script below is an opposite- variant of this script; while that one acts on specified drives, this scripts acts on all except specified (usb) drives. What it does Whenever an external usb storage device is being connected, the script copies its content into a directory, defined by you (in the head section of the script: target_folder =). A sub ...


2

rm $(ls -lo|grep -v [Pp][Dd][Ff]$|awk '{print $7}') Warning! Better try first ls -l $(ls -lo|grep -v [Pp][Dd][Ff]$|awk '{print $7}')


2

Since you did $(adduser ..) instead of simply adduser .., you apply command substitution and that line is replaced with the output of that command. So if adduser produces an output of Adding user ..., then the shell will try to execute Adding user ... as a command. Instead, do: id -u ${username} > /dev/null 2>&1 if [ $? -eq 1 ]; then echo ...


2

The script below watches a textfile that you can edit. If the file is changed, it will create a new layer over your wallpaper with the text of the file. Options you can define: text size text color number of columns (max) number of lines per column border width (around the text blocks) How to use The script uses Imagemagick, you might have to install ...


2

Be careful and compose: use xargs Here's an approach I like, because it lets me be very careful: compose a way to show just the files I want to delete, then send them to rm using xargs. For example: ls shows me everything ls | grep pdf shows me the files I want to keep. Hmm. ls | grep -v pdf shows the opposite: all except what I want to keep. In other ...


2

Move the entire bash command to a script (say, in /some/path/sync.sh): #! /bin/bash for i do EXT="${i##*.}" EXT=${EXT,,} if [[ "${EXT}" == *"/"* ]] then EXT="no_extension"; fi mkdir -p "${DEST}/${EXT}" rsync -a "$i" "${DEST}/${EXT}" done Then call the find thus: SRC="/src" export DEST="/dest" find "${SRC}" ...


2

Use this script: #! /bin/bash # for i in $(cat /etc/passwd | cut -d: -f1); do echo -n $i ": " grep $i /etc/group | cut -d: -f1 | tr "\n" " " echo done It will list all users in the system (included system) and print the list of groups near them. With a trivial modification you can print the numeric id too.


2

You can use find's -not or ! expressions: -not expr Same as ! expr, but not POSIX compliant. ! expr True if expr is false. This character will also usually need protection from interpretation by the shell. So, to move foobar.jpg but not the other two, you could use: find . -mindepth 3 -type f -regex ...


1

I don't know why, but this sounds like a XY-Problem... What is your real intention? Why don't you just link the first folder to the second one? What do you need the folder for if you don't want anything inside it? If you really want to move these files, you could e.g. create a cronjob, which regularly moves all files of the first folder to the second ...


1

My error was a logic error with -mtime. I thought my script was checking the names but -mtime checks for the date when the file was created. So my final code: DAYS_TO_KEEP=2 find $BACKUP_DIR -maxdepth 1 -mtime +"$DAYS_TO_KEEP" -exec rm -rf {} \; I just had to do a sudo touch -d "3 days ago" /filepath/folderToRemove to make -mname find the folder and make ...


1

As indicated by other answers, the result depends on whether anacron is installed. On a desktop, it is installed by default, BUT crucially it is not installed in the server distribution. So the answer is around 06:25 on a server and about 07:35 on a desktop.


1

I'm a bit late with this response, but the sensible way would be to do a ssh-keyscan on the new machine before you run the uptime gathering. ssh-keyscan <newhost> >> ~/.ssh/known_hosts Disabling the sanity check for convenience sake sounds like a bad plan, even if you think you're totally in control of the environment.


1

How about arrays? $ tree Desktop/ Documents/ Desktop/ └── my folder └── more └── file Documents/ └── my folder ├── folder └── more 5 directories, 1 file $ SEARCH=(D*/my\ folder) $ find "${SEARCH[@]}" Desktop/my folder Desktop/my folder/more Desktop/my folder/more/file Documents/my folder Documents/my folder/more Documents/my ...


1

What about creating the command lines you want to run, then running them? find . -name '*run.sh' -type f -exec \ echo 'c={}; cd ${c%/*} && nohup "./${c##*/}" &' \; If the output is what you want, just append | bash and run again.


1

Inspired by choroba's answer: find . -iname '*run.sh' -printf 'cd %h; ./%f\0' | xargs -L1 -0 -P0 bash -c You can use -printf to build the command line. %h is the directory where the file is located, and %f is the name of the file without the path (the basename). The -P option enables parallelism in xargs, and -L1 -0 makes it use one null-terminated line ...


1

best I could find on my own (and I'm a newbie) was just running 'apt-get check' for a clue as to how things turned out following an install. Also running 'script' prior to running an 'apt-get install' will capture all the output from the command to file so that you don't have to worry about it scrolling away.


1

Using wildcards in the sudoers file In the sudoers file, you can use wildcards (*), thus creating the possibility to run a script with arguments. An example I tested it with a shockingly simple python script. In the sudoers file I had to use the absolute path to python: /usr/bin/python in the line of the sudoers file: jacob ALL=NOPASSWD: ...


1

Try doing this using a here-document : vech=Bus echo $vech /bin/bash<<EOF echo "before empty line" echo $vech echo "after empty line" vech=Car echo $vech exit EOF echo $vech



Only top voted, non community-wiki answers of a minimum length are eligible