Questions tagged [unix-shell]
The unix-shell tag has no usage guidance.
132
questions
42
votes
4
answers
21k
views
Faking the date for a specific shell session
I'm certainly trying to achieve something weird here, but I want to fake the date locally for a shell session on GNU/Linux.
I need to black-box test how a program behaves at different dates, and ...
37
votes
6
answers
123k
views
compressing dd backup on the fly
Maybe this will sound like dumb question but the way i'm trying to do it doesn't work.
I'm on livecd, drive is unmounted, etc.
When i do backup this way
sudo dd if=/dev/sda2 of=/media/disk/sda2-...
34
votes
4
answers
55k
views
Append an echo to file?
In linux, how do I do something like
echo 'hello world' > log.txt
but instead of overwriting the contents of log.txt, it appends to the end of of log.txt?
31
votes
2
answers
46k
views
When to use single quotes, double quotes, or no quotes in grep?
While trying to search for a simple pattern "hello" in a file, all the following forms of grep work:
grep hello file1
grep 'hello' file1
grep "hello" file1
Is there a specific case where one of the ...
19
votes
8
answers
24k
views
portable unix way to join strings with separator
Is there a portable unix shellscripting way of joining a number of strings together with a given separator, like so:
$ strjoin --- foo bar baz quux
foo---bar---baz---quux
Sure I could use a $...
16
votes
7
answers
13k
views
In *nix, how to determine which filesystem a particular file is on?
In a generic, modern unix environment (say, GNU/Linux, GNU/Solaris, or Mac OS X), is there a good way to determine which mountpoint and filesystem-type a particular absolute file path is on?
I ...
14
votes
8
answers
8k
views
Deleting a UNIX directory with a hyphen in the name
Through a boneheaded maneuver on my part, I accidentally created a directory called (for instance) -A, and ended up filling it with files.
I want to delete said directory. I've tried:
rmdir -- -A
...
14
votes
5
answers
2k
views
Copy file permissions, but not files in Unix
(From : https://stackoverflow.com/questions/15245144/copy-file-permissions-but-not-files, closed because off-topic)
I have two copies of the same directory tree. They almost have the same files in ...
14
votes
2
answers
20k
views
cp -p will preserve times for some files but not all?
I have a source directory on a machine with a bunch of files, all with the same permissions, that I want to copy to another directory, that is mounted via SMB.
[tmark@dexter JR09141045 ...
13
votes
6
answers
35k
views
How do i find out which Fedora version I'm running from the cli (not uname -a)?
I want to know if the box is Fedora Core 4 or Redhat 9, or CentOS, etc... not if it has Kernel 2.6.x
12
votes
9
answers
41k
views
'Argument list too long' error while copying a large number of files
I am using the following command:
\cp -uf /home/ftpuser1/public_html/ftparea/*.jpg /home/ftpuser2/public_html/ftparea/
And I am getting the error:
-bash: /bin/cp: Argument list too long
I have ...
10
votes
1
answer
6k
views
Using a shell script to check if rsync made any changes
I've got a bash script that rsync's two directories. Sometimes there's a change, odds are there's nothing.
I want to run a command only if rsync actually made a change (add/update a file). ...
7
votes
2
answers
1k
views
When do file extensions override shebang lines on Linux?
Some PHP cron jobs started failing on a shared server recently. They had been working without error and no updates for nearly a year, but now would no longer run due to syntax errors. What was going ...
6
votes
3
answers
9k
views
How does cd command work? (unix) [closed]
Could anybody explain to me how cd - command work? man cd tells me that An argument of - is equivalent to $OLDPWD. Then I found on the net that $OLDPWD is the previous working directory as set by the ...
6
votes
2
answers
4k
views
How do I prevent bash $ substitution?
I'm trying to preserve $if below and
would like the following to output
$if
bash is getting in the way by doing
$ substitution
$ sed -e 's:\\$:\\$:g' <<ENDOFFILE
> $if
> ...
5
votes
6
answers
19k
views
How to check a file exists
How do I determine that a file exists using a shell script?
I.e:
#!/bin/sh
if [ Does File Exist? ]
then
do this thing
fi
5
votes
2
answers
40k
views
Kill all currently running cron jobs
For some reason my cron job scripts aren't exiting cleanly and they're backing up my server. There are currently a couple hundred processes running for one of my users. I can use the following command ...
5
votes
5
answers
4k
views
tcsh : path of sourced file
I am sourcing a file under tcsh. This file could be anywhere on the filesystem. How can I retrieve the path of my sourced file ?
$0 won't work : I don't execute the file, I source it.
Many thanks !
5
votes
4
answers
425
views
Is there a way to prevent output from backgrounded tasks from covering the command line in a shell?
I would like to be able to run task(s) in the background of a shell and not have their output to stdout or stderr cover the command line at the bottom. Frequently I need to run other commands to ...
4
votes
2
answers
12k
views
How to encode the rdesktop password parameter in a shell script?
I can connect with this script from linux to a windows RDP session:
#!/bin/bash
/usr/bin/rdesktop -N -x m -u Administrator -p secret123 -d DOMAIN1 127.0.0.1:33891
(The rdp port of the remote windows ...
4
votes
2
answers
5k
views
How to identify ancient KornShell version?
We had a guy retire on us, and the documentation left behind is a little on the light side. Need a little help identifying and using part an older client-server system.
The server itself is ...
4
votes
2
answers
1k
views
Shell scripting and regex: Which one is better to match numbers, [[:digit:]] or [0-9]?
As the title suggests, which one is better to match numbers, [[:digit:]] or [0-9]?
I'm using the bash shell
Thanks :)
4
votes
1
answer
375
views
"Threading" in Shell Scripts
I have a situation where I need to:
Do some preamble setup in Script A
Start Scripts B, C, D and E together, so they run concurrently
When all four scripts have exited, do some cleanup in Script A
...
3
votes
2
answers
195
views
supress expansion of * in echo
I am working on a script which dynamically executes some queries on daily basis. These queries are coming from a table in the database.
Here is the sample output of the query table:
+---------------+...
3
votes
3
answers
2k
views
sudo and sudo -i access features
What are privilege differences between sudo and sudo -i?
With sudo it's
$sudo echo "search foo.bar.baz" >> /etc/resolv.conf
bash: /etc/resolv.conf: Permission denied
...and with sudo -...
3
votes
3
answers
7k
views
Get results from grep in the order they appear?
I'm trying to grep a log file to only show lines that match a certain session ID. Thus far, it works great. However, when I get the results of my grep command, I'm not getting the entries in the ...
3
votes
1
answer
142
views
Editing the PATH in PASE
I am doing a some PHP development on an IBMi running ZendServer6. I have to call PHP by its full path of /usr/local/zendsvr6/bin/php-cli. Is there a dotfile I can edit in my home directory or some ...
3
votes
3
answers
5k
views
grep - recognize carriage return as new line
I want to search a webserver running unix for php-files containing a specific string. Usually I use these commands to accomplish this:
find . -name "*.php" -print0 | xargs -0 grep -H -i "the string ...
3
votes
2
answers
6k
views
How can I kill and wait for background processes to finish in a shell script when I Ctrl+C it?
I'm trying to set up a shell script so that it runs background processes, and when I ctrl+C the shell script, it kills the children, then exits.
The best that I've managed to come up with is this. It ...
3
votes
2
answers
2k
views
How to change VGA mode after boot and login with ubuntu?
Is there a way to change the screen resolution of the console after the system has been booted? I've seen plenty on setting the vga= line in the boot and in the grub config but I'd like to be able to ...
3
votes
5
answers
5k
views
Search and delete lines matching a pattern along with comments in previous line if any
I have a requirement to write a shell script in csh to search and delete lines matching a pattern along with comments in previous line if any. For example if my file has the following lines
Shell ...
2
votes
3
answers
598
views
putty aborts process while shutting down
I'm newby so the question is simple. After I connected to server via putty I want to execute some command. Suppose that command will be performed for long time (e.g. extracting files from large ...
2
votes
3
answers
9k
views
How to ls and know the target of symbolic links in one command line?
I need to get the folder contents in one command line, right now if I do stat or ls, it tells me file type is Symbolic Link but it doesn't tell me if it's a file or a folder.
I'm using this;
stat -c ...
2
votes
2
answers
14k
views
How to concatenate environment variable paths in a cleaner way in bash?
In order to modify my paths (like LD_LIBRARY_PATH or PYTHONPATH), I first check is the variable exists. If so, I'm concatenating my old value with the new one (separated with a semicolon), else I'm ...
2
votes
5
answers
14k
views
Counting number of items in a single-row CSV file?
I have a file containing a long comma-delimited list of numbers, like this:
2,8,42,75,101
What's the simplest command (from a Unix shell) to get the count of numbers in this file? In the example ...
2
votes
4
answers
1k
views
Is there an easy way to insert data/time in front of a log file with just shell scripting?
Say I want to monitor memory usage on a java process overnight.
I can do something like
top | grep java > out.log
I will get a log file with a whole bunch of lines.
Is there an easy way to get a ...
2
votes
1
answer
5k
views
Picking a single line from a text file by its line number
In a Unix shell, how can I pick a single line from a text file by its line number?
Say I want whatever is at line 3 in animals.txt to be written to stdout (bat bat bat).
monkey monkey monkey
cat cat ...
2
votes
1
answer
359
views
Find all the sym links that point to a particular file
I know that ls -l will give you the "number of links" but I'm looking for a command or combination of commands that will give me a list of all the symbolic links that point to a particular file.
2
votes
3
answers
4k
views
Unix Linux directory compare
I have a known good directory structure that I'd like to compare owner/group and permissions against another server and flag any files/folders that need to be changed. Since I wont have access to both ...
2
votes
2
answers
1k
views
Skeleton files /etc/skel/ automatic syncing of changes to all users
Linux distributions place some important scripts and config files in the skeleton, from where they are copied to each new user's home directory. Ideally system-wide files would be used instead of ...
2
votes
3
answers
2k
views
No shell and launch application on login
I am working on migrating an application from OpenVMS to RedHat Linux 6. The application is a green screen terminal application. The users will log into Linux via SSH and the application should ...
2
votes
1
answer
5k
views
How to pipe and echo the result that is being processed? [closed]
I'd like to execute this command:
find /apps/ -type f -print0 | grep -z log$ | xargs -0 grep 'this->aForm'
And in parallel, I'd like to see which files are being processed.
How to to this?
2
votes
1
answer
2k
views
'ls' taking a long time to list 20 subdirectories
I have a directory (my_dir) which has 20 sub directories. Every time I run 'ls' or try to autocomplete a file name in this directory my IO usage goes to 100% and it takes very long time (10 mins) to ...
2
votes
3
answers
152
views
Will uploading modified version of a KSH script affect processes that are currently running the script?
Let say I have a KSH script my_script.ksh that performs complex operations including but not limited to invoking SQLPLUS to manipulate large number (millions) of records in an Oracle database. The ...
2
votes
6
answers
439
views
Last command issued on a GNU/Linux box
Someone asked at the mailing list of my local LUG if there is any way of learning the last command issued on a GNU/Linux system.
We're talking last command in a system-wide fashion I guess. After ...
2
votes
2
answers
140
views
Make a gzip-compressed tar archive of files and directories that have a specific letter
I want ONLY directories and files that contain the letter.
I've been able to recreate it from a previous question. But the tar is not a gzip.
And when trying to make it a gzip it...doesn't remove ...
2
votes
2
answers
297
views
Rotatelogs rotating log files mid log entry
I have a c++ tool that outputs to STDOUT via printf i.e.
printf ("%s\n", logline);
I then pipe that to rotatelogs i.e.
tool | rotatelogs /tmp/logs/log_%s 60
Everything works great, but ...
2
votes
1
answer
9k
views
How to find a file that contains the specified string in FreeBSD?
I'm searching for the way to find a file that contains specified string text.
It should be fast as possible but its not that important.
I was reading the manual, and I've build something like that: ...
2
votes
0
answers
139
views
How to enable logging of commands run through shell script in ESXi Hypervisor?
On ESXi Hypervisor, If I run a command on the terminal via SSH, the commands are logged in the shell.log file, i.e if i run this command
[root@esxi04:~] ls -la
then I can see this command in shell.log ...
2
votes
1
answer
211
views
How to improve the efficiency of gnu parallel to read from a compressed stream?
Is another question extended from the previous one [1]
I have a compressed file and stream them to feed into a python program, e.g.
bzcat data.bz2 | parallel --no-notice -j16 --pipe python parse.py ...