Mycode prints many lines with 9 elements each. If the line (first element) starts with 'd', then do not print out the rest elements of the line. so if you try ls -l | mycode it will delete lines starting with 'd'. BUT for some reasons the elements move left by one if for example:
ls -l
drwxrwxrwx 2 alk lotus 35 Sep 23 19.00 directory1
-rwxrwxrwx 2 alk lotus 345 Sep 23 13.00 file1
drwxrwxrwx 3 alk lotus 245 Sep 23 19.20 directory2
drwxrwxrwx 24 alk lotus 15 Sep 23 12.00 directory3
-rwxrwxrwx 5 alk lotus 25 Sep 23 14.00 file2
-rwxrwxrwx 8 alk lotus 25 Sep 23 10.00 file3
ls -l | mycode
-rwxrwxrwx 2 alk lotus 345 Sep 23 13.00
file1 -rwxrwxrwx 5 alk lotus 25 Sep
23 14.00 file2 -rwxrwxrwx 8 alk lotus 25 Sep
23 10.00 file3
So why file2 file1 goes down? I wanna that file to stay in his place....After that all positions are mixed up there....Help please to fix this...
my code here:
The logic in your code seems not correct. What you want to do is to remove lines that starts with d, however in the code you it does something else.
I'm presenting you an example which is expected to work, which shall replace your code example:
while (fgets(string, 1024, stdin))
if (string[0] != 'd')
printf("%s", string); // don't use puts cuz fgets would store the newline
I don't understand why you aren't just using only fgets:
for (i=0; i<9;i++){
if (NULL != fgets(string,1024,stdin)) {
if (string[0] != 'd'){
printf("%s\n", string);
}
}
else break;
}
Related
I have these 2 space files in the below directory and want to remove space for these filenames
/home/sterlingadmin/
-rw-rw-r-- 1 sterlingadmin sterlingadmin 99 Jan 3 14:32 output test.txt
-rw-rw-r-- 1 sterlingadmin sterlingadmin 24 Dec 4 10:00 space test.txt
I try to use below find command to remove space for the files but its not reflecting for the files
[sterlingadmin#$
find -name "* *" |tr -d "[:blank:]"
./outputtest.txt
./delspace.txt
[sterlingadmin#$
ls -lrt
-rw-rw-r-- 1 sterlingadmin sterlingadmin 99 Jan 3 14:32 output test.txt
-rw-rw-r-- 1 sterlingadmin sterlingadmin 24 Dec 4 10:00 space test.txt'
Please advise if any solution for this issue. thank you.
i'm writing an implementation of ls command but i found a problem with
the columns, i want to align them like the real ls -l
drwx------# 3 haxor123 candidate 102 Oct 3 14:43 Applications
drwxr-xr-x 21 haxor123 candidate 714 Nov 29 21:07 Desktop
drwxr-xr-x 4 haxor123 candidate 136 Nov 6 19:54 Documents
drwx------ 9 haxor123 candidate 306 Nov 28 22:28 Downloads
drwxr-xr-x# 396 haxor123 candidate 13464 Nov 29 19:52 Library
drwx------+ 3 haxor123 candidate 102 Aug 9 16:38 Movies
drwx------+ 4 haxor123 candidate 136 Oct 5 14:13 Music
drwxr-xr-x 3 haxor123 candidate 102 Oct 4 23:23 PicineRe
drwxr-xr-x 4 haxor123 candidate 136 Oct 4 23:52 PicineRee
drwxr-xr-x 3 haxor123 candidate 102 Oct 4 22:32 PicineReloaded
drwx------+ 4 haxor123 candidate 136 Nov 11 16:46 Pictures
drwxr-xr-x 6 haxor123 candidate 204 Nov 12 21:38 exam-basedir
lrwxr-xr-x 1 haxor123 candidate 34 Jul 16 10:12 goinfre ->
/Volumes/Storage/goinfre/haxor123/
drwxr-xr-x 4 haxor123 candidate 136 Oct 3 15:14 s
That's a part from ls -l function
temp = list;
ft_putstr("total ");
printblocks(list);
ft_putchar('\n');
while (temp != NULL)
{
lstat(temp->full_path, &fstat);
ft_permissions(temp, fstat);
ft_putstr(" ");
bytes1 = ft_itoa(fstat.st_nlink);
ft_putstr(bytes1);
ft_putstr(get_user(fstat));
bytes = ft_itoa(fstat.st_size);
len = ft_strlen(bytes);
ft_putstr(ft_strjoin(bytes, " "));
get_time(fstat, temp);
temp = temp->next;
if (temp != NULL)
ft_putchar('\n');
If you would like to read the source code for GNU commands directly, you can do so...and it may be a good learning experience:
Where can I find source code for Linux core commands?
In particular, here is ls.c:
http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c
The only way to know precisely "how ls does it" comes from that file. We are not psychic (at least I am not) so if you're going to be asking about any other programming method, it needs to be self-contained in your question what you're specifically trying to achieve and why you can't achieve it.
here is part of code
scanf("%[^\n]%*c",command);
int pid;
pid=fork();
if (pid == 0) {
// Child process
char *argv[]={command ,NULL};
execvp(argv[0], argv);
exit (0);
}
When I give as input ls I want as output
1 copy of mysh1.c mysh1.c mysh3.c mysh.c New Folder
a.out helpmanual.desktop mysh2.c mysh4.c New File
and when i give ls -l /tmp
i'm waiting
total 12
-rw------- 1 antre antre 0 Nov 4 17:31 config-err-KT9sEZ
drwx------ 2 antre antre 4096 Nov 4 19:21 mozilla_antre0
drwx------ 2 antre antre 4096 Jan 1 1970 orbit-antre
drwx------ 2 antre antre 4096 Nov 4 17:31 ssh-HaOFtKdeIQnQ `
but i take:
1 copy of mysh1.c mysh1.c mysh3.c mysh.c New Folder
a.out helpmanual.desktop mysh2.c mysh4.c New File
It seems that you're trying to parse the output of ls -l in a C program for some reason.
That's unlikely to be the “right” thing to do. The usual mechanism is to use opendir and readdir to read the directory file, directly.
If you have some truly strange situation in which you cannot opendir (the only case that comes to mind is if you're running ls on a remote system, eg, over ssh), there is a mode in GNU ls specifically for producing an output record format that can be parsed by another program.
From the GNU coreutils info:
10.1.2 What information is listed
‘-D’
‘--dired’
With the long listing (‘-l’) format, print an additional line after
the main output:
//DIRED// BEG1 END1 BEG2 END2 ...
The BEGN and ENDN are unsigned integers that record the byte
position of the beginning and end of each file name in the output.
This makes it easy for Emacs to find the names, even when they
contain unusual characters such as space or newline, without fancy
searching.
If directories are being listed recursively (‘-R’), output a
similar line with offsets for each subdirectory name:
//SUBDIRED// BEG1 END1 ...
Finally, output a line of the form:
//DIRED-OPTIONS// --quoting-style=WORD
where WORD is the quoting style (*note Formatting the file
names::).
Here is an actual example:
$ mkdir -p a/sub/deeper a/sub2
$ touch a/f1 a/f2
$ touch a/sub/deeper/file
$ ls -gloRF --dired a
a:
total 8
-rw-r--r-- 1 0 Jun 10 12:27 f1
-rw-r--r-- 1 0 Jun 10 12:27 f2
drwxr-xr-x 3 4096 Jun 10 12:27 sub/
drwxr-xr-x 2 4096 Jun 10 12:27 sub2/
a/sub:
total 4
drwxr-xr-x 2 4096 Jun 10 12:27 deeper/
a/sub/deeper:
total 0
-rw-r--r-- 1 0 Jun 10 12:27 file
a/sub2:
total 0
//DIRED// 48 50 84 86 120 123 158 162 217 223 282 286
//SUBDIRED// 2 3 167 172 228 240 290 296
//DIRED-OPTIONS// --quoting-style=literal
Note that the pairs of offsets on the ‘//DIRED//’ line above
delimit these names: ‘f1’, ‘f2’, ‘sub’, ‘sub2’, ‘deeper’, ‘file’.
The offsets on the ‘//SUBDIRED//’ line delimit the following
directory names: ‘a’, ‘a/sub’, ‘a/sub/deeper’, ‘a/sub2’.
Here is an example of how to extract the fifth entry name,
‘deeper’, corresponding to the pair of offsets, 222 and 228:
$ ls -gloRF --dired a > out
$ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
deeper
Note that although the listing above includes a trailing slash for
the ‘deeper’ entry, the offsets select the name without the
trailing slash. However, if you invoke ‘ls’ with ‘--dired’ along
with an option like ‘--escape’ (aka ‘-b’) and operate on a file
whose name contains special characters, notice that the backslash
is included:
$ touch 'a b'
$ ls -blog --dired 'a b'
-rw-r--r-- 1 0 Jun 10 12:28 a\ b
//DIRED// 30 34
//DIRED-OPTIONS// --quoting-style=escape
If you use a quoting style that adds quote marks (e.g.,
‘--quoting-style=c’), then the offsets include the quote marks. So
beware that the user may select the quoting style via the
environment variable ‘QUOTING_STYLE’. Hence, applications using
‘--dired’ should either specify an explicit
‘--quoting-style=literal’ option (aka ‘-N’ or ‘--literal’) on the
command line, or else be prepared to parse the escaped names.
i just only needed to use strtok
When you do ls -la it returns each path along with info of whether or not it's a file/directory:
$ ls -la
drwxr-xr-x 11 viatropos staff 374 Jan 21 21:24 .
drwxr-xr-x 41 viatropos staff 1394 Feb 2 00:48 ..
-rw-r--r-- 1 viatropos staff 43 Jan 21 21:23 .gitignore
-rw-r--r-- 1 viatropos staff 43 Jan 21 21:23 .npmignore
-rw-r--r-- 1 viatropos staff 647 Jan 21 21:23 README.md
-rw-r--r-- 1 viatropos staff 3069 Feb 5 20:17 index.js
drwxr-xr-x 8 viatropos staff 272 Feb 5 20:06 node_modules
-rw-r--r-- 1 viatropos staff 291 Jan 21 21:24 package.json
drwxr-xr-x 4 viatropos staff 136 Jan 21 21:23 test
Is there a way to do this using the find command (and glob * functionality)? So, finding all paths within node_modules and having it return the path and whether or not it's a file directory? Something like:
$ find node_modules -name 'lib/*'
d node_modules/express/lib/
f node_modules/express/lib/index.js
...
How about find ... -printf '%y %p\n'? (This is probably a GNU find extension, though.)
Try this script, I called it "findfl". The "mtime" clause finds files changed in the last 3 days.
Directories will have "/" appended.
#!/bin/sh
# find files produced recently, matching input pattern
[ $1 ] || { echo "Usage: findfl <file-name-pattern>" ; exit ; }
TOPDIR=/home/usr/fred #the directory you want to search
echo "Searching $TOPDIR"
find . -mtime -3 -name *$1* 2>/dev/null | xargs -n 99 ls -lptr | sed "s! ./! $TOPDIR/!g"
I have a code (1) that is supposed to execute test in another code (its a pseudo-bash). This code (1) is wrote using the "expect" for the 'user simulation'.
The problem is when this code (1) execute a system ( on this case, system "rm totoExpect.txt titiExpect.txt") when its just doesnt find the titiExpected.txt, but is there!
There is nothing different between the two files, even the permissions are the same, i cant think in a reason that doesnt work.
Here is the part of the code (1) where the problem raises:
# test 5
proc t5 {} {
send "ls > totoExpect.txt\r"
send "cat < totoExpect.txt | wc -l > titiExpect.txt\r"
send "cat titiExpect.txt\r"
expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
system "rm totoExpect.txt titiExpect.txt"
}
And the Error message:
ls > totoExpect.txt
out: totoExpect.txt
seq[0]: 'ls'
-----3
ensishell>c
***** TEST 5 ok
rm: não foi possível remover "titiExpect.txt": Arquivo ou diretório não encontrado
child process exited abnormally
while executing
"system "rm totoExpect.txt titiExpect.txt""
(procedure "t5" line 6)
invoked from within
"t5"
("eval" body line 1)
invoked from within
"eval "t$t;" "
("foreach" body line 1)
invoked from within
"foreach t {0 1 2 3 4 5 6 7} { eval "t$t;" } "
invoked from within
"expect_user {
-timeout 30 "auto\n" {
puts "AUTO:\n";
foreach t {0 1 2 3 4 5 6 7} { eval "t$t;" }
}
-timeout 30 -re "(\[0123456789 \]+)\..."
(file "./testshell.expect" line 99)
make: ** [test] Erro 1
Where "rm: não foi possível remover "titiExpect.txt": Arquivo ou diretório não encontrado"
means "rm: it was not possible to remove "titiExpect.txt": file or directory not found" (sorry about that...)
and this is the ls -l just after the error message (so titiExpect.txt shouldnt be there):
-rwxrwxr-x 1 fernando fernando 273 Out 23 17:53 #Makefile#
-rwxrwxr-x 1 fernando fernando 6238 Nov 5 21:18 #ensishell.c#
-rwxrwxr-x 1 fernando fernando 1271 Out 24 20:30 #readcmd.h#
-rwxrwxr-x 1 fernando fernando 3250 Nov 5 21:07 #testshell.expect#
-rwxrwxrwx 1 fernando fernando 303 Out 24 20:21 Makefile
drwxrwxr-x 2 fernando fernando 4096 Nov 4 19:06 SEPC shell
-rw-rw-r-- 1 fernando fernando 193453 Out 18 18:25 Sujet_shell.pdf
-rwxrwxr-x 1 fernando fernando 25451 Nov 5 21:17 ensishell
-rwxrwxrwx 1 fernando fernando 6238 Nov 5 20:32 ensishell.c
-rw-rw-r-- 1 fernando fernando 10664 Nov 5 21:17 ensishell.o
-rwxrwxr-x 1 fernando fernando 7251 Nov 4 00:33 foo
-rw-rw-r-- 1 fernando fernando 173 Nov 4 00:33 foo.c
-rw-rw-r-- 1 fernando fernando 16 Nov 4 01:15 in.txt~
-rwxrwxrwx 1 fernando fernando 6603 Out 23 00:37 readcmd.c
-rwxrwxrwx 1 fernando fernando 1271 Out 23 01:24 readcmd.h
-rwxrwxrwx 1 fernando fernando 1271 Out 23 00:37 readcmd.h~
-rw-rw-r-- 1 fernando fernando 11216 Nov 5 21:17 readcmd.o
-rwxrwxrwx 1 fernando fernando 3250 Nov 5 20:41 testshell.expect
-rwx------ 1 fernando fernando 1263 Nov 5 12:43 toto.txt
The worst problem is this code is not supposed to be modified, but its seems to me that is the programs fault this fail. (in fact, commenting the line solve the problem..)
Any ideas?
Look.
# test 5
proc t5 {} {
send "ls > totoExpect.txt\r"
send "cat < totoExpect.txt | wc -l > titiExpect.txt\r"
send "cat titiExpect.txt\r"
expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
system "rm totoExpect.txt titiExpect.txt"
}
The first send crates a file named totoExpect.txt\r.
The second send generates a file named titiExpect.txt\r. The cat part actually fails, because there is no file totoExpect.txt, but since the command is part of a pipe, and not the last command in said pipe, expect will not catch that as an error. (All you'll see is that the titiExpect.txt\r file will be empty.)
The \r above is the CR character, and probably the reason you have missed it. In Linux, it is perfectly allowed character in file names (as only / and \0 are forbidden). Just remove it from your test and you'll find it works fine.
Or, if you insist on keeping it, then keep it consistently:
# test 5
proc t5 {} {
send "ls > totoExpect.txt\r"
send "cat < totoExpect.txt\r | wc -l > titiExpect.txt\r"
send "cat titiExpect.txt\r"
expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
system "rm totoExpect.txt\r titiExpect.txt\r"
}
Finally, when removing files, it is recommended to use the -f flag, so rm does not complain if one of the files happens to not exist.
My suggestion is to rewrite that test as
# test 5
proc t5 {} {
send "ls > totoExpect.txt"
send "cat < totoExpect.txt | wc -l > titiExpect.txt"
send "cat titiExpect.txt"
expect -re "(0|1|2|3|4|5|6|7|8|9)+.*>" { ok 5; } default { abort 5 }
system "rm -f totoExpect.txt titiExpect.txt"
}
eradicating those erratic \rs.